blob: e120153be19534b4d064b988c4408c1d35f430c7 [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) :
82 OptionValueProperties(*global_properties->GetValueProperties())
83 {
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 Clayton73844aa2012-08-22 17:17:09 +0000142//ThreadInstanceSettings (GetSettingsController()),
Greg Claytonf4124de2012-02-21 00:09:25 +0000143 m_process_wp (process_sp),
Greg Clayton643ee732010-08-04 01:40:35 +0000144 m_actual_stop_info_sp (),
Greg Claytonf4124de2012-02-21 00:09:25 +0000145 m_index_id (process_sp->GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +0000146 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000147 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000148 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000149 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000150 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000151 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000152 m_curr_frames_sp (),
153 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000154 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000155 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000156 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000157 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000158 m_destroy_called (false),
159 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +0000160
Chris Lattner24943d22010-06-08 16:52:24 +0000161{
Greg Claytone005f2c2010-11-06 01:53:30 +0000162 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000163 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000164 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000165
166 QueueFundamentalPlan(true);
Greg Clayton73844aa2012-08-22 17:17:09 +0000167 //UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000168}
169
170
171Thread::~Thread()
172{
Greg Claytone005f2c2010-11-06 01:53:30 +0000173 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000174 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000175 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +0000176 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
177 assert (m_destroy_called);
178}
179
180void
181Thread::DestroyThread ()
182{
183 m_plan_stack.clear();
184 m_discarded_plan_stack.clear();
185 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +0000186 m_actual_stop_info_sp.reset();
Jim Inghamcdea2362010-11-18 02:47:07 +0000187 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000188}
189
Jim Ingham6297a3a2010-10-20 00:39:53 +0000190lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000191Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000192{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000193 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000194 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000195 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000196 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000197 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000198 ProcessSP process_sp (GetProcess());
199 if (process_sp
200 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000201 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000202 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000203 return m_actual_stop_info_sp;
204 else
205 return GetPrivateStopReason ();
206 }
Chris Lattner24943d22010-06-08 16:52:24 +0000207}
208
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000209void
210Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
211{
212 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000213 if (m_actual_stop_info_sp)
214 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000215 ProcessSP process_sp (GetProcess());
216 if (process_sp)
217 m_thread_stop_reason_stop_id = process_sp->GetStopID();
218 else
219 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000220}
221
222void
223Thread::SetStopInfoToNothing()
224{
225 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
226 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
227 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
228}
229
Chris Lattner24943d22010-06-08 16:52:24 +0000230bool
231Thread::ThreadStoppedForAReason (void)
232{
Jim Ingham9880efa2012-08-11 00:35:26 +0000233 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000234}
235
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000236bool
237Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
238{
239 if (!SaveFrameZeroState(saved_state.register_backup))
240 return false;
241
242 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000243 ProcessSP process_sp (GetProcess());
244 if (process_sp)
245 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000246 return true;
247}
248
249bool
250Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
251{
252 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000253 if (saved_state.stop_info_sp)
254 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000255 SetStopInfo(saved_state.stop_info_sp);
256 return true;
257}
258
Chris Lattner24943d22010-06-08 16:52:24 +0000259StateType
260Thread::GetState() const
261{
262 // If any other threads access this we will need a mutex for it
263 Mutex::Locker locker(m_state_mutex);
264 return m_state;
265}
266
267void
268Thread::SetState(StateType state)
269{
270 Mutex::Locker locker(m_state_mutex);
271 m_state = state;
272}
273
274void
275Thread::WillStop()
276{
277 ThreadPlan *current_plan = GetCurrentPlan();
278
279 // FIXME: I may decide to disallow threads with no plans. In which
280 // case this should go to an assert.
281
282 if (!current_plan)
283 return;
284
285 current_plan->WillStop();
286}
287
288void
289Thread::SetupForResume ()
290{
291 if (GetResumeState() != eStateSuspended)
292 {
293
294 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
295 // telling the current plan it will resume, since we might change what the current
296 // plan is.
297
298 lldb::addr_t pc = GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +0000299 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000300 if (bp_site_sp && bp_site_sp->IsEnabled())
301 {
302 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
303 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000304
305 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000306
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000307 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
308 {
309 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
310 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000311 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000312 ThreadPlanSP step_bp_plan_sp;
313 step_bp_plan->SetPrivate (true);
314
Chris Lattner24943d22010-06-08 16:52:24 +0000315 if (GetCurrentPlan()->RunState() != eStateStepping)
316 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000317 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000318 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000319 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000320 QueueThreadPlan (step_bp_plan_sp, false);
321 }
322 }
323 }
324 }
325}
326
327bool
328Thread::WillResume (StateType resume_state)
329{
330 // At this point clear the completed plan stack.
331 m_completed_plan_stack.clear();
332 m_discarded_plan_stack.clear();
333
Jim Ingham149d1f52012-01-31 23:09:20 +0000334 SetTemporaryResumeState(resume_state);
335
336 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
337 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
338 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
339 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000340 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000341 if (m_thread_stop_reason_stop_id == process_stop_id &&
342 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
343 {
344 StopInfo *stop_info = GetPrivateStopReason().get();
345 if (stop_info)
346 stop_info->WillResume (resume_state);
347 }
Chris Lattner24943d22010-06-08 16:52:24 +0000348
349 // Tell all the plans that we are about to resume in case they need to clear any state.
350 // We distinguish between the plan on the top of the stack and the lower
351 // plans in case a plan needs to do any special business before it runs.
352
353 ThreadPlan *plan_ptr = GetCurrentPlan();
354 plan_ptr->WillResume(resume_state, true);
355
356 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
357 {
358 plan_ptr->WillResume (resume_state, false);
359 }
Greg Clayton643ee732010-08-04 01:40:35 +0000360
Greg Clayton643ee732010-08-04 01:40:35 +0000361 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000362 return true;
363}
364
365void
366Thread::DidResume ()
367{
368 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
369}
370
371bool
372Thread::ShouldStop (Event* event_ptr)
373{
374 ThreadPlan *current_plan = GetCurrentPlan();
375 bool should_stop = true;
376
Greg Claytone005f2c2010-11-06 01:53:30 +0000377 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000378
379 if (GetResumeState () == eStateSuspended)
380 {
381 if (log)
382 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
383 __FUNCTION__,
384 GetID ());
385// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
386// __FUNCTION__,
387// GetID (),
388// GetRegisterContext()->GetPC());
389 return false;
390 }
391
392 if (GetTemporaryResumeState () == eStateSuspended)
393 {
394 if (log)
395 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
396 __FUNCTION__,
397 GetID ());
398// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
399// __FUNCTION__,
400// GetID (),
401// GetRegisterContext()->GetPC());
402 return false;
403 }
404
405 if (ThreadStoppedForAReason() == false)
406 {
407 if (log)
408 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
409 __FUNCTION__,
410 GetID (),
411 GetRegisterContext()->GetPC());
412 return false;
413 }
414
Chris Lattner24943d22010-06-08 16:52:24 +0000415 if (log)
416 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000417 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
418 __FUNCTION__,
419 GetID (),
420 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000421 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000422 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000423 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000424 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000425 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000426 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000427
428 // The top most plan always gets to do the trace log...
429 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000430
431 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
432 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
433 // do any more work on this stop.
434 StopInfoSP private_stop_info (GetPrivateStopReason());
435 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
436 {
437 if (log)
438 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
439 return false;
440 }
Chris Lattner24943d22010-06-08 16:52:24 +0000441
Jim Inghamad382c52011-12-03 01:52:59 +0000442 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
443 // If that plan is still working, then we don't need to do any more work. If the plan that explains
444 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
445 // whether they still need to do more work.
446
447 bool done_processing_current_plan = false;
448
449 if (!current_plan->PlanExplainsStop())
450 {
451 if (current_plan->TracerExplainsStop())
452 {
453 done_processing_current_plan = true;
454 should_stop = false;
455 }
456 else
457 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000458 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000459 // does and let it handle the situation.
460 ThreadPlan *plan_ptr = current_plan;
461 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
462 {
463 if (plan_ptr->PlanExplainsStop())
464 {
465 should_stop = plan_ptr->ShouldStop (event_ptr);
466
467 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
468 // and all the plans below it off the stack.
469
470 if (plan_ptr->MischiefManaged())
471 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000472 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000473 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000474
475 do
476 {
477 if (should_stop)
478 current_plan->WillStop();
479 PopPlan();
480 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000481 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
482 // Now, if the responsible plan was not "Okay to discard" then we're done,
483 // otherwise we forward this to the next plan in the stack below.
484 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
485 done_processing_current_plan = true;
486 else
487 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000488 }
489 else
490 done_processing_current_plan = true;
491
492 break;
493 }
494
495 }
496 }
497 }
498
499 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000500 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000501 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000502
Jim Inghame8f4e112011-10-15 00:23:43 +0000503 if (log)
504 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
505
Jim Inghamf9f40c22011-02-08 05:20:59 +0000506 // We're starting from the base plan, so just let it decide;
507 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000508 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000509 should_stop = current_plan->ShouldStop (event_ptr);
510 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000511 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000512 }
513 else
514 {
515 // Otherwise, don't let the base plan override what the other plans say to do, since
516 // presumably if there were other plans they would know what to do...
517 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000518 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000519 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000520 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000521
522 should_stop = current_plan->ShouldStop(event_ptr);
523 if (log)
524 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
525 if (current_plan->MischiefManaged())
526 {
527 if (should_stop)
528 current_plan->WillStop();
529
530 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
531 // Otherwise, see if the plan's parent wants to stop.
532
533 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
534 {
535 PopPlan();
536 break;
537 }
538 else
539 {
540
541 PopPlan();
542
543 current_plan = GetCurrentPlan();
544 if (current_plan == NULL)
545 {
546 break;
547 }
548 }
Chris Lattner24943d22010-06-08 16:52:24 +0000549 }
550 else
551 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000552 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000553 }
Chris Lattner24943d22010-06-08 16:52:24 +0000554 }
555 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000556
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000557 if (over_ride_stop)
558 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000559
560 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
561 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
562 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
563 // This code clears stale plans off the stack.
564
565 if (should_stop)
566 {
567 ThreadPlan *plan_ptr = GetCurrentPlan();
568 while (!PlanIsBasePlan(plan_ptr))
569 {
570 bool stale = plan_ptr->IsPlanStale ();
571 ThreadPlan *examined_plan = plan_ptr;
572 plan_ptr = GetPreviousPlan (examined_plan);
573
574 if (stale)
575 {
576 if (log)
577 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
578 DiscardThreadPlansUpToPlan(examined_plan);
579 }
580 }
581 }
582
Chris Lattner24943d22010-06-08 16:52:24 +0000583 }
Chris Lattner24943d22010-06-08 16:52:24 +0000584
Jim Inghame8f4e112011-10-15 00:23:43 +0000585 if (log)
586 {
587 StreamString s;
588 s.IndentMore();
589 DumpThreadPlans(&s);
590 log->Printf ("Plan stack final state:\n%s", s.GetData());
591 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
592 }
Chris Lattner24943d22010-06-08 16:52:24 +0000593 return should_stop;
594}
595
596Vote
597Thread::ShouldReportStop (Event* event_ptr)
598{
599 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000600 StateType temp_thread_state = GetTemporaryResumeState();
601
Greg Claytone005f2c2010-11-06 01:53:30 +0000602 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000603
604 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
605 {
606 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000607 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 +0000608 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000609 }
Chris Lattner24943d22010-06-08 16:52:24 +0000610
Jim Ingham149d1f52012-01-31 23:09:20 +0000611 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
612 {
613 if (log)
614 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
615 return eVoteNoOpinion;
616 }
617
618 if (!ThreadStoppedForAReason())
619 {
620 if (log)
621 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
622 return eVoteNoOpinion;
623 }
624
Chris Lattner24943d22010-06-08 16:52:24 +0000625 if (m_completed_plan_stack.size() > 0)
626 {
627 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000628 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000629 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 +0000630 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
631 }
632 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000633 {
634 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000635 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000636 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000637 }
Chris Lattner24943d22010-06-08 16:52:24 +0000638}
639
640Vote
641Thread::ShouldReportRun (Event* event_ptr)
642{
643 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000644
Chris Lattner24943d22010-06-08 16:52:24 +0000645 if (thread_state == eStateSuspended
646 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000647 {
Chris Lattner24943d22010-06-08 16:52:24 +0000648 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000649 }
650
651 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000652 if (m_completed_plan_stack.size() > 0)
653 {
654 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000655 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000656 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 +0000657 GetIndexID(),
658 GetID(),
659 m_completed_plan_stack.back()->GetName());
660
Chris Lattner24943d22010-06-08 16:52:24 +0000661 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
662 }
663 else
Jim Inghamac959662011-01-24 06:34:17 +0000664 {
665 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000666 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 +0000667 GetIndexID(),
668 GetID(),
669 GetCurrentPlan()->GetName());
670
Chris Lattner24943d22010-06-08 16:52:24 +0000671 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000672 }
Chris Lattner24943d22010-06-08 16:52:24 +0000673}
674
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000675bool
676Thread::MatchesSpec (const ThreadSpec *spec)
677{
678 if (spec == NULL)
679 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000680
Jim Inghama2664912012-03-07 22:03:04 +0000681 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000682}
683
Chris Lattner24943d22010-06-08 16:52:24 +0000684void
685Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
686{
687 if (thread_plan_sp)
688 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000689 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
690 if (!thread_plan_sp->GetThreadPlanTracer())
691 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000692 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000693
Chris Lattner24943d22010-06-08 16:52:24 +0000694 thread_plan_sp->DidPush();
695
Greg Claytone005f2c2010-11-06 01:53:30 +0000696 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000697 if (log)
698 {
699 StreamString s;
700 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000701 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000702 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000703 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000704 }
705 }
706}
707
708void
709Thread::PopPlan ()
710{
Greg Claytone005f2c2010-11-06 01:53:30 +0000711 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000712
Jim Ingham6297a3a2010-10-20 00:39:53 +0000713 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000714 return;
715 else
716 {
717 ThreadPlanSP &plan = m_plan_stack.back();
718 if (log)
719 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000720 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000721 }
722 m_completed_plan_stack.push_back (plan);
723 plan->WillPop();
724 m_plan_stack.pop_back();
725 }
726}
727
728void
729Thread::DiscardPlan ()
730{
731 if (m_plan_stack.size() > 1)
732 {
733 ThreadPlanSP &plan = m_plan_stack.back();
734 m_discarded_plan_stack.push_back (plan);
735 plan->WillPop();
736 m_plan_stack.pop_back();
737 }
738}
739
740ThreadPlan *
741Thread::GetCurrentPlan ()
742{
Jim Ingham2f41d272012-04-19 00:17:05 +0000743 // There will always be at least the base plan. If somebody is mucking with a
744 // thread with an empty plan stack, we should assert right away.
745 assert (!m_plan_stack.empty());
746
747 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000748}
749
750ThreadPlanSP
751Thread::GetCompletedPlan ()
752{
753 ThreadPlanSP empty_plan_sp;
754 if (!m_completed_plan_stack.empty())
755 {
756 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
757 {
758 ThreadPlanSP completed_plan_sp;
759 completed_plan_sp = m_completed_plan_stack[i];
760 if (!completed_plan_sp->GetPrivate ())
761 return completed_plan_sp;
762 }
763 }
764 return empty_plan_sp;
765}
766
Jim Ingham1586d972011-12-17 01:35:57 +0000767ValueObjectSP
768Thread::GetReturnValueObject ()
769{
770 if (!m_completed_plan_stack.empty())
771 {
772 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
773 {
774 ValueObjectSP return_valobj_sp;
775 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
776 if (return_valobj_sp)
777 return return_valobj_sp;
778 }
779 }
780 return ValueObjectSP();
781}
782
Chris Lattner24943d22010-06-08 16:52:24 +0000783bool
784Thread::IsThreadPlanDone (ThreadPlan *plan)
785{
Chris Lattner24943d22010-06-08 16:52:24 +0000786 if (!m_completed_plan_stack.empty())
787 {
788 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
789 {
790 if (m_completed_plan_stack[i].get() == plan)
791 return true;
792 }
793 }
794 return false;
795}
796
797bool
798Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
799{
Chris Lattner24943d22010-06-08 16:52:24 +0000800 if (!m_discarded_plan_stack.empty())
801 {
802 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
803 {
804 if (m_discarded_plan_stack[i].get() == plan)
805 return true;
806 }
807 }
808 return false;
809}
810
811ThreadPlan *
812Thread::GetPreviousPlan (ThreadPlan *current_plan)
813{
814 if (current_plan == NULL)
815 return NULL;
816
817 int stack_size = m_completed_plan_stack.size();
818 for (int i = stack_size - 1; i > 0; i--)
819 {
820 if (current_plan == m_completed_plan_stack[i].get())
821 return m_completed_plan_stack[i-1].get();
822 }
823
824 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
825 {
Chris Lattner24943d22010-06-08 16:52:24 +0000826 if (m_plan_stack.size() > 0)
827 return m_plan_stack.back().get();
828 else
829 return NULL;
830 }
831
832 stack_size = m_plan_stack.size();
833 for (int i = stack_size - 1; i > 0; i--)
834 {
835 if (current_plan == m_plan_stack[i].get())
836 return m_plan_stack[i-1].get();
837 }
838 return NULL;
839}
840
841void
842Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
843{
844 if (abort_other_plans)
845 DiscardThreadPlans(true);
846
847 PushPlan (thread_plan_sp);
848}
849
Jim Ingham745ac7a2010-11-11 19:26:09 +0000850
851void
852Thread::EnableTracer (bool value, bool single_stepping)
853{
854 int stack_size = m_plan_stack.size();
855 for (int i = 0; i < stack_size; i++)
856 {
857 if (m_plan_stack[i]->GetThreadPlanTracer())
858 {
859 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
860 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
861 }
862 }
863}
864
865void
866Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
867{
868 int stack_size = m_plan_stack.size();
869 for (int i = 0; i < stack_size; i++)
870 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
871}
872
Chris Lattner24943d22010-06-08 16:52:24 +0000873void
Jim Inghamea9d4262010-11-05 19:25:48 +0000874Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
875{
Jim Ingham88e3de22012-05-03 21:19:36 +0000876 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
877}
878
879void
880Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
881{
Greg Claytone005f2c2010-11-06 01:53:30 +0000882 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000883 if (log)
884 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000885 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 +0000886 }
887
888 int stack_size = m_plan_stack.size();
889
890 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
891 // stack, and if so discard up to and including it.
892
Jim Ingham88e3de22012-05-03 21:19:36 +0000893 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000894 {
895 for (int i = stack_size - 1; i > 0; i--)
896 DiscardPlan();
897 }
898 else
899 {
900 bool found_it = false;
901 for (int i = stack_size - 1; i > 0; i--)
902 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000903 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000904 found_it = true;
905 }
906 if (found_it)
907 {
908 bool last_one = false;
909 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
910 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000911 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000912 last_one = true;
913 DiscardPlan();
914 }
915 }
916 }
917 return;
918}
919
920void
Chris Lattner24943d22010-06-08 16:52:24 +0000921Thread::DiscardThreadPlans(bool force)
922{
Greg Claytone005f2c2010-11-06 01:53:30 +0000923 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000924 if (log)
925 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000926 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000927 }
928
929 if (force)
930 {
931 int stack_size = m_plan_stack.size();
932 for (int i = stack_size - 1; i > 0; i--)
933 {
934 DiscardPlan();
935 }
936 return;
937 }
938
939 while (1)
940 {
941
942 int master_plan_idx;
943 bool discard;
944
945 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
946 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
947 {
948 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
949 {
950 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
951 break;
952 }
953 }
954
955 if (discard)
956 {
957 // First pop all the dependent plans:
958 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
959 {
960
961 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
962 // for the plan leaves it in a state that it is safe to pop the plan
963 // with no more notice?
964 DiscardPlan();
965 }
966
967 // Now discard the master plan itself.
968 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
969 // discard it's dependent plans, but not it...
970 if (master_plan_idx > 0)
971 {
972 DiscardPlan();
973 }
974 }
975 else
976 {
977 // If the master plan doesn't want to get discarded, then we're done.
978 break;
979 }
980
981 }
Chris Lattner24943d22010-06-08 16:52:24 +0000982}
983
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000984bool
985Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
986{
987 if (plan_ptr->IsBasePlan())
988 return true;
989 else if (m_plan_stack.size() == 0)
990 return false;
991 else
992 return m_plan_stack[0].get() == plan_ptr;
993}
994
Chris Lattner24943d22010-06-08 16:52:24 +0000995ThreadPlan *
996Thread::QueueFundamentalPlan (bool abort_other_plans)
997{
998 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
999 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1000 return thread_plan_sp.get();
1001}
1002
1003ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001004Thread::QueueThreadPlanForStepSingleInstruction
1005(
1006 bool step_over,
1007 bool abort_other_plans,
1008 bool stop_other_threads
1009)
Chris Lattner24943d22010-06-08 16:52:24 +00001010{
1011 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1012 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1013 return thread_plan_sp.get();
1014}
1015
1016ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001017Thread::QueueThreadPlanForStepRange
1018(
1019 bool abort_other_plans,
1020 StepType type,
1021 const AddressRange &range,
1022 const SymbolContext &addr_context,
1023 lldb::RunMode stop_other_threads,
1024 bool avoid_code_without_debug_info
1025)
Chris Lattner24943d22010-06-08 16:52:24 +00001026{
1027 ThreadPlanSP thread_plan_sp;
1028 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001029 {
1030 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1031 if (avoid_code_without_debug_info)
1032 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1033 else
1034 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1035 thread_plan_sp.reset (plan);
1036 }
Chris Lattner24943d22010-06-08 16:52:24 +00001037 else
1038 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1039
1040 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1041 return thread_plan_sp.get();
1042}
1043
1044
1045ThreadPlan *
1046Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1047{
1048 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1049 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1050 return thread_plan_sp.get();
1051}
1052
1053ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001054Thread::QueueThreadPlanForStepOut
1055(
1056 bool abort_other_plans,
1057 SymbolContext *addr_context,
1058 bool first_insn,
1059 bool stop_other_threads,
1060 Vote stop_vote,
1061 Vote run_vote,
1062 uint32_t frame_idx
1063)
Chris Lattner24943d22010-06-08 16:52:24 +00001064{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001065 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1066 addr_context,
1067 first_insn,
1068 stop_other_threads,
1069 stop_vote,
1070 run_vote,
1071 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001072
1073 if (thread_plan_sp->ValidatePlan(NULL))
1074 {
1075 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1076 return thread_plan_sp.get();
1077 }
1078 else
1079 {
1080 return NULL;
1081 }
Chris Lattner24943d22010-06-08 16:52:24 +00001082}
1083
1084ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001085Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001086{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001087 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001088 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1089 return NULL;
1090
Chris Lattner24943d22010-06-08 16:52:24 +00001091 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1092 return thread_plan_sp.get();
1093}
1094
1095ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001096Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1097 Address& function,
1098 lldb::addr_t arg,
1099 bool stop_other_threads,
1100 bool discard_on_error)
1101{
Jim Ingham016ef882011-12-22 19:12:40 +00001102 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001103 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1104 return thread_plan_sp.get();
1105}
1106
1107ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001108Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1109 Address &target_addr,
1110 bool stop_other_threads)
1111{
1112 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1113 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1114 return thread_plan_sp.get();
1115}
1116
1117ThreadPlan *
1118Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001119 lldb::addr_t *address_list,
1120 size_t num_addresses,
1121 bool stop_other_threads,
1122 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001123{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001124 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001125 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1126 return thread_plan_sp.get();
1127
1128}
1129
1130uint32_t
1131Thread::GetIndexID () const
1132{
1133 return m_index_id;
1134}
1135
1136void
1137Thread::DumpThreadPlans (lldb_private::Stream *s) const
1138{
1139 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001140 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001141 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001142 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 +00001143 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001144 {
Chris Lattner24943d22010-06-08 16:52:24 +00001145 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001146 s->Indent();
1147 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001148 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001149 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001150 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001151 }
1152
Chris Lattner24943d22010-06-08 16:52:24 +00001153 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001154 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001155 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001156 s->Indent();
1157 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1158 for (i = stack_size - 1; i >= 0; i--)
1159 {
1160 s->IndentMore();
1161 s->Indent();
1162 s->Printf ("Element %d: ", i);
1163 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1164 s->EOL();
1165 s->IndentLess();
1166 }
Chris Lattner24943d22010-06-08 16:52:24 +00001167 }
1168
1169 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001170 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001171 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001172 s->Indent();
1173 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1174 for (i = stack_size - 1; i >= 0; i--)
1175 {
1176 s->IndentMore();
1177 s->Indent();
1178 s->Printf ("Element %d: ", i);
1179 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1180 s->EOL();
1181 s->IndentLess();
1182 }
Chris Lattner24943d22010-06-08 16:52:24 +00001183 }
1184
1185}
1186
Greg Clayton289afcb2012-02-18 05:35:26 +00001187TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001188Thread::CalculateTarget ()
1189{
Greg Claytonf4124de2012-02-21 00:09:25 +00001190 TargetSP target_sp;
1191 ProcessSP process_sp(GetProcess());
1192 if (process_sp)
1193 target_sp = process_sp->CalculateTarget();
1194 return target_sp;
1195
Chris Lattner24943d22010-06-08 16:52:24 +00001196}
1197
Greg Clayton289afcb2012-02-18 05:35:26 +00001198ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001199Thread::CalculateProcess ()
1200{
Greg Claytonf4124de2012-02-21 00:09:25 +00001201 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001202}
1203
Greg Clayton289afcb2012-02-18 05:35:26 +00001204ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001205Thread::CalculateThread ()
1206{
Greg Clayton289afcb2012-02-18 05:35:26 +00001207 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001208}
1209
Greg Clayton289afcb2012-02-18 05:35:26 +00001210StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001211Thread::CalculateStackFrame ()
1212{
Greg Clayton289afcb2012-02-18 05:35:26 +00001213 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001214}
1215
1216void
Greg Claytona830adb2010-10-04 01:05:56 +00001217Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001218{
Greg Claytonf4124de2012-02-21 00:09:25 +00001219 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001220}
1221
Greg Clayton782b9cc2010-08-25 00:35:26 +00001222
Greg Clayton2450cb12012-03-29 01:41:38 +00001223StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001224Thread::GetStackFrameList ()
1225{
Greg Clayton2450cb12012-03-29 01:41:38 +00001226 StackFrameListSP frame_list_sp;
1227 Mutex::Locker locker(m_frame_mutex);
1228 if (m_curr_frames_sp)
1229 {
1230 frame_list_sp = m_curr_frames_sp;
1231 }
1232 else
1233 {
1234 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1235 m_curr_frames_sp = frame_list_sp;
1236 }
1237 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001238}
1239
Greg Clayton782b9cc2010-08-25 00:35:26 +00001240void
1241Thread::ClearStackFrames ()
1242{
Greg Clayton2450cb12012-03-29 01:41:38 +00001243 Mutex::Locker locker(m_frame_mutex);
1244
Jim Inghambf97d742012-02-29 03:40:22 +00001245 // Only store away the old "reference" StackFrameList if we got all its frames:
1246 // FIXME: At some point we can try to splice in the frames we have fetched into
1247 // the new frame as we make it, but let's not try that now.
1248 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001249 m_prev_frames_sp.swap (m_curr_frames_sp);
1250 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001251}
1252
1253lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001254Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1255{
Greg Clayton2450cb12012-03-29 01:41:38 +00001256 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001257}
1258
Chris Lattner24943d22010-06-08 16:52:24 +00001259void
Greg Claytona830adb2010-10-04 01:05:56 +00001260Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001261{
Greg Claytonf4124de2012-02-21 00:09:25 +00001262 ExecutionContext exe_ctx (shared_from_this());
1263 Process *process = exe_ctx.GetProcessPtr();
1264 if (process == NULL)
1265 return;
1266
Greg Clayton567e7f32011-09-22 04:58:26 +00001267 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001268 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001269 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001270 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001271 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001272 if (frame_sp)
1273 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001274 exe_ctx.SetFrameSP(frame_sp);
1275 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001276 }
1277 }
1278
Greg Claytonf4124de2012-02-21 00:09:25 +00001279 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001280 assert (thread_format);
1281 const char *end = NULL;
1282 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001283 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001284 &exe_ctx,
1285 NULL,
1286 strm,
1287 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001288}
1289
Greg Clayton990de7b2010-11-18 23:32:35 +00001290void
Caroline Tice2a456812011-03-10 22:14:10 +00001291Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001292{
Greg Clayton73844aa2012-08-22 17:17:09 +00001293// UserSettingsController::InitializeSettingsController (GetSettingsController(),
1294// SettingsController::global_settings_table,
1295// SettingsController::instance_settings_table);
1296//
Caroline Tice2a456812011-03-10 22:14:10 +00001297 // Now call SettingsInitialize() on each 'child' setting of Thread.
1298 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +00001299}
Jim Ingham20594b12010-09-08 03:14:33 +00001300
Greg Clayton990de7b2010-11-18 23:32:35 +00001301void
Caroline Tice2a456812011-03-10 22:14:10 +00001302Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001303{
Caroline Tice2a456812011-03-10 22:14:10 +00001304 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
1305 // Currently there are none.
1306
1307 // Now terminate Thread Settings.
Greg Clayton73844aa2012-08-22 17:17:09 +00001308//
1309// UserSettingsControllerSP &usc = GetSettingsController();
1310// UserSettingsController::FinalizeSettingsController (usc);
1311// usc.reset();
Greg Clayton990de7b2010-11-18 23:32:35 +00001312}
Jim Ingham20594b12010-09-08 03:14:33 +00001313
Greg Clayton73844aa2012-08-22 17:17:09 +00001314//UserSettingsControllerSP &
1315//Thread::GetSettingsController ()
1316//{
1317// static UserSettingsControllerSP g_settings_controller_sp;
1318// if (!g_settings_controller_sp)
1319// {
1320// g_settings_controller_sp.reset (new Thread::SettingsController);
1321// // The first shared pointer to Target::SettingsController in
1322// // g_settings_controller_sp must be fully created above so that
1323// // the TargetInstanceSettings can use a weak_ptr to refer back
1324// // to the master setttings controller
1325// InstanceSettingsSP default_instance_settings_sp (new ThreadInstanceSettings (g_settings_controller_sp,
1326// false,
1327// InstanceSettings::GetDefaultName().AsCString()));
1328//
1329// g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1330// }
1331// return g_settings_controller_sp;
1332//}
Greg Clayton334d33a2012-01-30 07:41:31 +00001333
Greg Clayton73844aa2012-08-22 17:17:09 +00001334//void
1335//Thread::UpdateInstanceName ()
1336//{
1337// StreamString sstr;
1338// const char *name = GetName();
1339//
1340// if (name && name[0] != '\0')
1341// sstr.Printf ("%s", name);
1342// else if ((GetIndexID() != 0) || (GetID() != 0))
1343// sstr.Printf ("0x%4.4x", GetIndexID());
1344//
1345// if (sstr.GetSize() > 0)
1346// Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1347//}
Caroline Tice1ebef442010-09-27 00:30:10 +00001348
Jim Inghamccd584d2010-09-23 17:40:12 +00001349lldb::StackFrameSP
1350Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1351{
Greg Clayton2450cb12012-03-29 01:41:38 +00001352 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001353}
Caroline Tice7826c882010-10-26 03:11:13 +00001354
1355const char *
1356Thread::StopReasonAsCString (lldb::StopReason reason)
1357{
1358 switch (reason)
1359 {
1360 case eStopReasonInvalid: return "invalid";
1361 case eStopReasonNone: return "none";
1362 case eStopReasonTrace: return "trace";
1363 case eStopReasonBreakpoint: return "breakpoint";
1364 case eStopReasonWatchpoint: return "watchpoint";
1365 case eStopReasonSignal: return "signal";
1366 case eStopReasonException: return "exception";
1367 case eStopReasonPlanComplete: return "plan complete";
1368 }
1369
1370
1371 static char unknown_state_string[64];
1372 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1373 return unknown_state_string;
1374}
1375
1376const char *
1377Thread::RunModeAsCString (lldb::RunMode mode)
1378{
1379 switch (mode)
1380 {
1381 case eOnlyThisThread: return "only this thread";
1382 case eAllThreads: return "all threads";
1383 case eOnlyDuringStepping: return "only during stepping";
1384 }
1385
1386 static char unknown_state_string[64];
1387 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1388 return unknown_state_string;
1389}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001390
Greg Claytonabe0fed2011-04-18 08:33:37 +00001391size_t
1392Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1393{
Greg Claytonf4124de2012-02-21 00:09:25 +00001394 ExecutionContext exe_ctx (shared_from_this());
1395 Target *target = exe_ctx.GetTargetPtr();
1396 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001397 size_t num_frames_shown = 0;
1398 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001399 bool is_selected = false;
1400 if (process)
1401 {
1402 if (process->GetThreadList().GetSelectedThread().get() == this)
1403 is_selected = true;
1404 }
1405 strm.Printf("%c ", is_selected ? '*' : ' ');
1406 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001407 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001408 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001409 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001410 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001411 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1412 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1413 {
1414 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1415 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001416 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001417 }
1418
1419 DumpUsingSettingsFormat (strm, start_frame);
1420
1421 if (num_frames > 0)
1422 {
1423 strm.IndentMore();
1424
1425 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001426 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001427 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1428 start_frame,
1429 num_frames,
1430 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001431 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001432 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001433 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001434 }
1435 return num_frames_shown;
1436}
1437
1438size_t
1439Thread::GetStackFrameStatus (Stream& strm,
1440 uint32_t first_frame,
1441 uint32_t num_frames,
1442 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001443 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001444{
Greg Clayton2450cb12012-03-29 01:41:38 +00001445 return GetStackFrameList()->GetStatus (strm,
1446 first_frame,
1447 num_frames,
1448 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001449 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001450}
1451
Peter Collingbournee426d852011-06-03 20:40:54 +00001452bool
1453Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1454{
1455 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1456 if (frame_sp)
1457 {
1458 checkpoint.SetStackID(frame_sp->GetStackID());
1459 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1460 }
1461 return false;
1462}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001463
Peter Collingbournee426d852011-06-03 20:40:54 +00001464bool
1465Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1466{
1467 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1468 if (frame_sp)
1469 {
1470 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1471
1472 // Clear out all stack frames as our world just changed.
1473 ClearStackFrames();
1474 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1475
1476 return ret;
1477 }
1478 return false;
1479}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001480
Greg Clayton37f962e2011-08-22 02:49:39 +00001481Unwind *
1482Thread::GetUnwinder ()
1483{
1484 if (m_unwinder_ap.get() == NULL)
1485 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001486 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001487 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1488 switch (machine)
1489 {
1490 case llvm::Triple::x86_64:
1491 case llvm::Triple::x86:
1492 case llvm::Triple::arm:
1493 case llvm::Triple::thumb:
1494 m_unwinder_ap.reset (new UnwindLLDB (*this));
1495 break;
1496
1497 default:
1498 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1499 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1500 break;
1501 }
1502 }
1503 return m_unwinder_ap.get();
1504}
1505
1506
Greg Claytoncf5927e2012-05-18 02:38:05 +00001507void
1508Thread::Flush ()
1509{
1510 ClearStackFrames ();
1511 m_reg_context_sp.reset();
1512}
1513
1514
Greg Clayton990de7b2010-11-18 23:32:35 +00001515#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001516//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001517// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001518//--------------------------------------------------------------
Greg Clayton73844aa2012-08-22 17:17:09 +00001519//
1520//Thread::SettingsController::SettingsController () :
1521// UserSettingsController ("thread", Process::GetSettingsController())
1522//{
1523//}
1524//
1525//Thread::SettingsController::~SettingsController ()
1526//{
1527//}
1528//
1529//lldb::InstanceSettingsSP
1530//Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
1531//{
1532// lldb::InstanceSettingsSP new_settings_sp (new ThreadInstanceSettings (GetSettingsController(),
1533// false,
1534// instance_name));
1535// return new_settings_sp;
1536//}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001537
Greg Clayton73844aa2012-08-22 17:17:09 +00001538//#pragma mark "ThreadInstanceSettings"
1539////--------------------------------------------------------------
1540//// class ThreadInstanceSettings
1541////--------------------------------------------------------------
1542//
1543//ThreadInstanceSettings::ThreadInstanceSettings (const UserSettingsControllerSP &owner_sp, bool live_instance, const char *name) :
1544// InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1545// m_avoid_regexp_ap (),
1546// m_trace_enabled (false)
1547//{
1548// // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1549// // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1550// // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1551// // This is true for CreateInstanceName() too.
1552//
1553// if (GetInstanceName() == InstanceSettings::InvalidName())
1554// {
1555// ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1556// owner_sp->RegisterInstanceSettings (this);
1557// }
1558//
1559// if (live_instance)
1560// {
1561// CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
1562// }
1563//}
1564//
1565//ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
1566// InstanceSettings (Thread::GetSettingsController(), CreateInstanceName().AsCString()),
1567// m_avoid_regexp_ap (),
1568// m_trace_enabled (rhs.m_trace_enabled)
1569//{
1570// if (m_instance_name != InstanceSettings::GetDefaultName())
1571// {
1572// UserSettingsControllerSP owner_sp (m_owner_wp.lock());
1573// if (owner_sp)
1574// {
1575// CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name), false);
1576// owner_sp->RemovePendingSettings (m_instance_name);
1577// }
1578// }
1579// if (rhs.m_avoid_regexp_ap.get() != NULL)
1580// m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1581//}
1582//
1583//ThreadInstanceSettings::~ThreadInstanceSettings ()
1584//{
1585//}
1586//
1587//ThreadInstanceSettings&
1588//ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1589//{
1590// if (this != &rhs)
1591// {
1592// if (rhs.m_avoid_regexp_ap.get() != NULL)
1593// m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1594// else
1595// m_avoid_regexp_ap.reset(NULL);
1596// }
1597// m_trace_enabled = rhs.m_trace_enabled;
1598// return *this;
1599//}
1600//
1601//
1602//void
1603//ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1604// const char *index_value,
1605// const char *value,
1606// const ConstString &instance_name,
1607// const SettingEntry &entry,
1608// VarSetOperationType op,
1609// Error &err,
1610// bool pending)
1611//{
1612// if (var_name == StepAvoidRegexpVarName())
1613// {
1614// std::string regexp_text;
1615// if (m_avoid_regexp_ap.get() != NULL)
1616// regexp_text.append (m_avoid_regexp_ap->GetText());
1617// UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1618// if (regexp_text.empty())
1619// m_avoid_regexp_ap.reset();
1620// else
1621// {
1622// m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1623//
1624// }
1625// }
1626// else if (var_name == GetTraceThreadVarName())
1627// {
1628// bool success;
1629// bool result = Args::StringToBoolean(value, false, &success);
1630//
1631// if (success)
1632// {
1633// m_trace_enabled = result;
1634// if (!pending)
1635// {
1636// Thread *myself = static_cast<Thread *> (this);
1637// myself->EnableTracer(m_trace_enabled, true);
1638// }
1639// }
1640// else
1641// {
1642// err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1643// }
1644//
1645// }
1646//}
1647//
1648//void
1649//ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1650// bool pending)
1651//{
1652// if (new_settings.get() == NULL)
1653// return;
1654//
1655// ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1656// if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1657// m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1658// else
1659// m_avoid_regexp_ap.reset ();
1660//}
1661//
1662//bool
1663//ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1664// const ConstString &var_name,
1665// StringList &value,
1666// Error *err)
1667//{
1668// if (var_name == StepAvoidRegexpVarName())
1669// {
1670// if (m_avoid_regexp_ap.get() != NULL)
1671// {
1672// std::string regexp_text("\"");
1673// regexp_text.append(m_avoid_regexp_ap->GetText());
1674// regexp_text.append ("\"");
1675// value.AppendString (regexp_text.c_str());
1676// }
1677//
1678// }
1679// else if (var_name == GetTraceThreadVarName())
1680// {
1681// value.AppendString(m_trace_enabled ? "true" : "false");
1682// }
1683// else
1684// {
1685// if (err)
1686// err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1687// return false;
1688// }
1689// return true;
1690//}
1691//
1692//const ConstString
1693//ThreadInstanceSettings::CreateInstanceName ()
1694//{
1695// static int instance_count = 1;
1696// StreamString sstr;
1697//
1698// sstr.Printf ("thread_%d", instance_count);
1699// ++instance_count;
1700//
1701// const ConstString ret_val (sstr.GetData());
1702// return ret_val;
1703//}
1704//
1705//const ConstString &
1706//ThreadInstanceSettings::StepAvoidRegexpVarName ()
1707//{
1708// static ConstString step_avoid_var_name ("step-avoid-regexp");
1709//
1710// return step_avoid_var_name;
1711//}
1712//
1713//const ConstString &
1714//ThreadInstanceSettings::GetTraceThreadVarName ()
1715//{
1716// static ConstString trace_thread_var_name ("trace-thread");
1717//
1718// return trace_thread_var_name;
1719//}
1720//
Jim Ingham745ac7a2010-11-11 19:26:09 +00001721//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001722// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001723//--------------------------------------------------
Greg Clayton73844aa2012-08-22 17:17:09 +00001724//
1725//SettingEntry
1726//Thread::SettingsController::global_settings_table[] =
1727//{
1728// //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1729// { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1730//};
1731//
1732//
1733//SettingEntry
1734//Thread::SettingsController::instance_settings_table[] =
1735//{
1736// //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1737// { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1738// { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1739// { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1740//};