blob: 5a48a0ad4a392572d003ccc48fb4add86a772a35 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-private-log.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000011#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Claytona830adb2010-10-04 01:05:56 +000012#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Log.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/StreamString.h"
Jim Ingham20594b12010-09-08 03:14:33 +000016#include "lldb/Core/RegularExpression.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000017#include "lldb/Host/Host.h"
Jim Ingham46657452012-09-27 01:15:29 +000018#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Target/DynamicLoader.h"
20#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000021#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000024#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000025#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Target/Thread.h"
27#include "lldb/Target/ThreadPlan.h"
28#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/ThreadPlanBase.h"
30#include "lldb/Target/ThreadPlanStepInstruction.h"
31#include "lldb/Target/ThreadPlanStepOut.h"
32#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
33#include "lldb/Target/ThreadPlanStepThrough.h"
34#include "lldb/Target/ThreadPlanStepInRange.h"
35#include "lldb/Target/ThreadPlanStepOverRange.h"
36#include "lldb/Target/ThreadPlanRunToAddress.h"
37#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000038#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000039#include "lldb/Target/Unwind.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000040#include "Plugins/Process/Utility/UnwindLLDB.h"
41#include "UnwindMacOSXFrameBackchain.h"
42
Chris Lattner24943d22010-06-08 16:52:24 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton73844aa2012-08-22 17:17:09 +000047
48const ThreadPropertiesSP &
49Thread::GetGlobalProperties()
50{
51 static ThreadPropertiesSP g_settings_sp;
52 if (!g_settings_sp)
53 g_settings_sp.reset (new ThreadProperties (true));
54 return g_settings_sp;
55}
56
57static PropertyDefinition
58g_properties[] =
59{
60 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
61 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
62 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
63};
64
65enum {
66 ePropertyStepAvoidRegex,
67 ePropertyEnableThreadTrace
68};
69
70
71class ThreadOptionValueProperties : public OptionValueProperties
72{
73public:
74 ThreadOptionValueProperties (const ConstString &name) :
75 OptionValueProperties (name)
76 {
77 }
78
79 // This constructor is used when creating ThreadOptionValueProperties when it
80 // is part of a new lldb_private::Thread instance. It will copy all current
81 // global property values as needed
82 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Claytonc6e82e42012-08-22 18:39:03 +000083 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton73844aa2012-08-22 17:17:09 +000084 {
85 }
86
87 virtual const Property *
88 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
89 {
90 // When gettings the value for a key from the thread options, we will always
91 // try and grab the setting from the current thread if there is one. Else we just
92 // use the one from this instance.
93 if (exe_ctx)
94 {
95 Thread *thread = exe_ctx->GetThreadPtr();
96 if (thread)
97 {
98 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
99 if (this != instance_properties)
100 return instance_properties->ProtectedGetPropertyAtIndex (idx);
101 }
102 }
103 return ProtectedGetPropertyAtIndex (idx);
104 }
105};
106
107
108
109ThreadProperties::ThreadProperties (bool is_global) :
110 Properties ()
111{
112 if (is_global)
113 {
114 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
115 m_collection_sp->Initialize(g_properties);
116 }
117 else
118 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
119}
120
121ThreadProperties::~ThreadProperties()
122{
123}
124
125const RegularExpression *
126ThreadProperties::GetSymbolsToAvoidRegexp()
127{
128 const uint32_t idx = ePropertyStepAvoidRegex;
129 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
130}
131
132bool
133ThreadProperties::GetTraceEnabledState() const
134{
135 const uint32_t idx = ePropertyEnableThreadTrace;
136 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
137}
138
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000139//------------------------------------------------------------------
140// Thread Event Data
141//------------------------------------------------------------------
Greg Clayton73844aa2012-08-22 17:17:09 +0000142
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000143
144const ConstString &
145Thread::ThreadEventData::GetFlavorString ()
146{
147 static ConstString g_flavor ("Thread::ThreadEventData");
148 return g_flavor;
149}
150
151Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
152 m_thread_sp (thread_sp),
153 m_stack_id ()
154{
155}
156
157Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
158 m_thread_sp (thread_sp),
159 m_stack_id (stack_id)
160{
161}
162
163Thread::ThreadEventData::ThreadEventData () :
164 m_thread_sp (),
165 m_stack_id ()
166{
167}
168
169Thread::ThreadEventData::~ThreadEventData ()
170{
171}
172
173void
174Thread::ThreadEventData::Dump (Stream *s) const
175{
176
177}
178
179const Thread::ThreadEventData *
180Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
181{
182 if (event_ptr)
183 {
184 const EventData *event_data = event_ptr->GetData();
185 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
186 return static_cast <const ThreadEventData *> (event_ptr->GetData());
187 }
188 return NULL;
189}
190
191ThreadSP
192Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
193{
194 ThreadSP thread_sp;
195 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
196 if (event_data)
197 thread_sp = event_data->GetThread();
198 return thread_sp;
199}
200
201StackID
202Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
203{
204 StackID stack_id;
205 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
206 if (event_data)
207 stack_id = event_data->GetStackID();
208 return stack_id;
209}
210
211StackFrameSP
212Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
213{
214 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
215 StackFrameSP frame_sp;
216 if (event_data)
217 {
218 ThreadSP thread_sp = event_data->GetThread();
219 if (thread_sp)
220 {
221 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
222 }
223 }
224 return frame_sp;
225}
226
227//------------------------------------------------------------------
228// Thread class
229//------------------------------------------------------------------
230
231ConstString &
232Thread::GetStaticBroadcasterClass ()
233{
234 static ConstString class_name ("lldb.thread");
235 return class_name;
236}
237
238Thread::Thread (Process &process, lldb::tid_t tid) :
Greg Clayton73844aa2012-08-22 17:17:09 +0000239 ThreadProperties (false),
Chris Lattner24943d22010-06-08 16:52:24 +0000240 UserID (tid),
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000241 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
242 m_process_wp (process.shared_from_this()),
Greg Clayton643ee732010-08-04 01:40:35 +0000243 m_actual_stop_info_sp (),
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000244 m_index_id (process.GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +0000245 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000246 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000247 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000248 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000249 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000250 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000251 m_curr_frames_sp (),
252 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000253 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000254 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000255 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000256 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000257 m_destroy_called (false),
258 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +0000259
Chris Lattner24943d22010-06-08 16:52:24 +0000260{
Greg Claytone005f2c2010-11-06 01:53:30 +0000261 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000262 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000263 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000264
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000265 CheckInWithManager();
Chris Lattner24943d22010-06-08 16:52:24 +0000266 QueueFundamentalPlan(true);
267}
268
269
270Thread::~Thread()
271{
Greg Claytone005f2c2010-11-06 01:53:30 +0000272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000273 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000274 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +0000275 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
276 assert (m_destroy_called);
277}
278
279void
280Thread::DestroyThread ()
281{
282 m_plan_stack.clear();
283 m_discarded_plan_stack.clear();
284 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +0000285 m_actual_stop_info_sp.reset();
Jim Inghamcdea2362010-11-18 02:47:07 +0000286 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000287}
288
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000289void
290Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
291{
292 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
293 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
294}
295
296uint32_t
297Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
298{
299 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
300 if (broadcast)
301 BroadcastSelectedFrameChange(frame->GetStackID());
302 return ret_value;
303}
304
305bool
306Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
307{
308 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
309 if (frame_sp)
310 {
311 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
312 if (broadcast)
313 BroadcastSelectedFrameChange(frame_sp->GetStackID());
314 return true;
315 }
316 else
317 return false;
318}
319
320
Jim Ingham6297a3a2010-10-20 00:39:53 +0000321lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000322Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000323{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000324 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000325 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000326 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000327 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000328 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000329 ProcessSP process_sp (GetProcess());
330 if (process_sp
331 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000332 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000333 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000334 return m_actual_stop_info_sp;
335 else
336 return GetPrivateStopReason ();
337 }
Chris Lattner24943d22010-06-08 16:52:24 +0000338}
339
Greg Clayton3acaa922012-09-25 02:40:06 +0000340lldb::StopReason
341Thread::GetStopReason()
342{
343 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
344 if (stop_info_sp)
Filipe Cabecinhasc50a1dd2012-09-28 15:55:43 +0000345 return stop_info_sp->GetStopReason();
Greg Clayton3acaa922012-09-25 02:40:06 +0000346 return eStopReasonNone;
347}
348
349
350
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000351void
352Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
353{
354 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000355 if (m_actual_stop_info_sp)
356 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000357 ProcessSP process_sp (GetProcess());
358 if (process_sp)
359 m_thread_stop_reason_stop_id = process_sp->GetStopID();
360 else
361 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000362}
363
364void
365Thread::SetStopInfoToNothing()
366{
367 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
368 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
369 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
370}
371
Chris Lattner24943d22010-06-08 16:52:24 +0000372bool
373Thread::ThreadStoppedForAReason (void)
374{
Jim Ingham9880efa2012-08-11 00:35:26 +0000375 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000376}
377
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000378bool
379Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
380{
381 if (!SaveFrameZeroState(saved_state.register_backup))
382 return false;
383
384 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000385 ProcessSP process_sp (GetProcess());
386 if (process_sp)
387 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham36de3c02012-09-07 23:36:43 +0000388 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
389
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000390 return true;
391}
392
393bool
394Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
395{
396 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000397 if (saved_state.stop_info_sp)
398 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000399 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham36de3c02012-09-07 23:36:43 +0000400 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000401 return true;
402}
403
Chris Lattner24943d22010-06-08 16:52:24 +0000404StateType
405Thread::GetState() const
406{
407 // If any other threads access this we will need a mutex for it
408 Mutex::Locker locker(m_state_mutex);
409 return m_state;
410}
411
412void
413Thread::SetState(StateType state)
414{
415 Mutex::Locker locker(m_state_mutex);
416 m_state = state;
417}
418
419void
420Thread::WillStop()
421{
422 ThreadPlan *current_plan = GetCurrentPlan();
423
424 // FIXME: I may decide to disallow threads with no plans. In which
425 // case this should go to an assert.
426
427 if (!current_plan)
428 return;
429
430 current_plan->WillStop();
431}
432
433void
434Thread::SetupForResume ()
435{
436 if (GetResumeState() != eStateSuspended)
437 {
438
439 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
440 // telling the current plan it will resume, since we might change what the current
441 // plan is.
442
443 lldb::addr_t pc = GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +0000444 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000445 if (bp_site_sp && bp_site_sp->IsEnabled())
446 {
447 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
448 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000449
450 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000451
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000452 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
453 {
454 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
455 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000456 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000457 ThreadPlanSP step_bp_plan_sp;
458 step_bp_plan->SetPrivate (true);
459
Chris Lattner24943d22010-06-08 16:52:24 +0000460 if (GetCurrentPlan()->RunState() != eStateStepping)
461 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000462 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000463 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000464 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000465 QueueThreadPlan (step_bp_plan_sp, false);
466 }
467 }
468 }
469 }
470}
471
472bool
473Thread::WillResume (StateType resume_state)
474{
475 // At this point clear the completed plan stack.
476 m_completed_plan_stack.clear();
477 m_discarded_plan_stack.clear();
478
Greg Clayton3acaa922012-09-25 02:40:06 +0000479 m_temporary_resume_state = resume_state;
Jim Ingham149d1f52012-01-31 23:09:20 +0000480
481 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
482 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
483 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
484 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000485 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000486 if (m_thread_stop_reason_stop_id == process_stop_id &&
487 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
488 {
489 StopInfo *stop_info = GetPrivateStopReason().get();
490 if (stop_info)
491 stop_info->WillResume (resume_state);
492 }
Chris Lattner24943d22010-06-08 16:52:24 +0000493
494 // Tell all the plans that we are about to resume in case they need to clear any state.
495 // We distinguish between the plan on the top of the stack and the lower
496 // plans in case a plan needs to do any special business before it runs.
497
498 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000499 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000500
501 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
502 {
503 plan_ptr->WillResume (resume_state, false);
504 }
Greg Clayton643ee732010-08-04 01:40:35 +0000505
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000506 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
507 // In that case, don't reset it here.
508
509 if (need_to_resume)
510 {
511 m_actual_stop_info_sp.reset();
512 }
513
514 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000515}
516
517void
518Thread::DidResume ()
519{
520 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
521}
522
523bool
524Thread::ShouldStop (Event* event_ptr)
525{
526 ThreadPlan *current_plan = GetCurrentPlan();
527 bool should_stop = true;
528
Greg Claytone005f2c2010-11-06 01:53:30 +0000529 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000530
531 if (GetResumeState () == eStateSuspended)
532 {
533 if (log)
534 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
535 __FUNCTION__,
536 GetID ());
537// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
538// __FUNCTION__,
539// GetID (),
540// GetRegisterContext()->GetPC());
541 return false;
542 }
543
544 if (GetTemporaryResumeState () == eStateSuspended)
545 {
546 if (log)
547 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
548 __FUNCTION__,
549 GetID ());
550// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
551// __FUNCTION__,
552// GetID (),
553// GetRegisterContext()->GetPC());
554 return false;
555 }
556
557 if (ThreadStoppedForAReason() == false)
558 {
559 if (log)
560 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
561 __FUNCTION__,
562 GetID (),
563 GetRegisterContext()->GetPC());
564 return false;
565 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000566
Chris Lattner24943d22010-06-08 16:52:24 +0000567 if (log)
568 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000569 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
570 __FUNCTION__,
571 GetID (),
572 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000573 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000574 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000575 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000576 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000577 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000578 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000579
580 // The top most plan always gets to do the trace log...
581 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000582
583 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
584 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
585 // do any more work on this stop.
586 StopInfoSP private_stop_info (GetPrivateStopReason());
587 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
588 {
589 if (log)
590 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
591 return false;
592 }
Chris Lattner24943d22010-06-08 16:52:24 +0000593
Jim Ingham98d50212012-09-05 21:12:49 +0000594 // If we've already been restarted, don't query the plans since the state they would examine is not current.
595 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
596 return false;
597
598 // Before the plans see the state of the world, calculate the current inlined depth.
599 GetStackFrameList()->CalculateCurrentInlinedDepth();
600
Jim Inghamad382c52011-12-03 01:52:59 +0000601 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
602 // If that plan is still working, then we don't need to do any more work. If the plan that explains
603 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
604 // whether they still need to do more work.
605
606 bool done_processing_current_plan = false;
607
608 if (!current_plan->PlanExplainsStop())
609 {
610 if (current_plan->TracerExplainsStop())
611 {
612 done_processing_current_plan = true;
613 should_stop = false;
614 }
615 else
616 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000617 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000618 // does and let it handle the situation.
619 ThreadPlan *plan_ptr = current_plan;
620 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
621 {
622 if (plan_ptr->PlanExplainsStop())
623 {
624 should_stop = plan_ptr->ShouldStop (event_ptr);
625
626 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
627 // and all the plans below it off the stack.
628
629 if (plan_ptr->MischiefManaged())
630 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000631 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000632 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000633
634 do
635 {
636 if (should_stop)
637 current_plan->WillStop();
638 PopPlan();
639 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000640 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
641 // Now, if the responsible plan was not "Okay to discard" then we're done,
642 // otherwise we forward this to the next plan in the stack below.
643 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
644 done_processing_current_plan = true;
645 else
646 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000647 }
648 else
649 done_processing_current_plan = true;
650
651 break;
652 }
653
654 }
655 }
656 }
657
658 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000659 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000660 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000661
Jim Inghame8f4e112011-10-15 00:23:43 +0000662 if (log)
663 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
664
Jim Inghamf9f40c22011-02-08 05:20:59 +0000665 // We're starting from the base plan, so just let it decide;
666 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000667 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000668 should_stop = current_plan->ShouldStop (event_ptr);
669 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000670 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000671 }
672 else
673 {
674 // Otherwise, don't let the base plan override what the other plans say to do, since
675 // presumably if there were other plans they would know what to do...
676 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000677 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000678 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000679 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000680
681 should_stop = current_plan->ShouldStop(event_ptr);
682 if (log)
683 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
684 if (current_plan->MischiefManaged())
685 {
686 if (should_stop)
687 current_plan->WillStop();
688
689 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
690 // Otherwise, see if the plan's parent wants to stop.
691
692 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
693 {
694 PopPlan();
695 break;
696 }
697 else
698 {
699
700 PopPlan();
701
702 current_plan = GetCurrentPlan();
703 if (current_plan == NULL)
704 {
705 break;
706 }
707 }
Chris Lattner24943d22010-06-08 16:52:24 +0000708 }
709 else
710 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000711 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000712 }
Chris Lattner24943d22010-06-08 16:52:24 +0000713 }
714 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000715
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000716 if (over_ride_stop)
717 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000718
719 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
720 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
721 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
722 // This code clears stale plans off the stack.
723
724 if (should_stop)
725 {
726 ThreadPlan *plan_ptr = GetCurrentPlan();
727 while (!PlanIsBasePlan(plan_ptr))
728 {
729 bool stale = plan_ptr->IsPlanStale ();
730 ThreadPlan *examined_plan = plan_ptr;
731 plan_ptr = GetPreviousPlan (examined_plan);
732
733 if (stale)
734 {
735 if (log)
736 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
737 DiscardThreadPlansUpToPlan(examined_plan);
738 }
739 }
740 }
741
Chris Lattner24943d22010-06-08 16:52:24 +0000742 }
Chris Lattner24943d22010-06-08 16:52:24 +0000743
Jim Inghame8f4e112011-10-15 00:23:43 +0000744 if (log)
745 {
746 StreamString s;
747 s.IndentMore();
748 DumpThreadPlans(&s);
749 log->Printf ("Plan stack final state:\n%s", s.GetData());
750 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
751 }
Chris Lattner24943d22010-06-08 16:52:24 +0000752 return should_stop;
753}
754
755Vote
756Thread::ShouldReportStop (Event* event_ptr)
757{
758 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000759 StateType temp_thread_state = GetTemporaryResumeState();
760
Greg Claytone005f2c2010-11-06 01:53:30 +0000761 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000762
763 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
764 {
765 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000766 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 +0000767 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000768 }
Chris Lattner24943d22010-06-08 16:52:24 +0000769
Jim Ingham149d1f52012-01-31 23:09:20 +0000770 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
771 {
772 if (log)
773 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
774 return eVoteNoOpinion;
775 }
776
777 if (!ThreadStoppedForAReason())
778 {
779 if (log)
780 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
781 return eVoteNoOpinion;
782 }
783
Chris Lattner24943d22010-06-08 16:52:24 +0000784 if (m_completed_plan_stack.size() > 0)
785 {
786 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000787 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000788 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 +0000789 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
790 }
791 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000792 {
793 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000794 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000795 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000796 }
Chris Lattner24943d22010-06-08 16:52:24 +0000797}
798
799Vote
800Thread::ShouldReportRun (Event* event_ptr)
801{
802 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000803
Chris Lattner24943d22010-06-08 16:52:24 +0000804 if (thread_state == eStateSuspended
805 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000806 {
Chris Lattner24943d22010-06-08 16:52:24 +0000807 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000808 }
809
810 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000811 if (m_completed_plan_stack.size() > 0)
812 {
813 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000814 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000815 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 +0000816 GetIndexID(),
817 GetID(),
818 m_completed_plan_stack.back()->GetName());
819
Chris Lattner24943d22010-06-08 16:52:24 +0000820 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
821 }
822 else
Jim Inghamac959662011-01-24 06:34:17 +0000823 {
824 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000825 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 +0000826 GetIndexID(),
827 GetID(),
828 GetCurrentPlan()->GetName());
829
Chris Lattner24943d22010-06-08 16:52:24 +0000830 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000831 }
Chris Lattner24943d22010-06-08 16:52:24 +0000832}
833
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000834bool
835Thread::MatchesSpec (const ThreadSpec *spec)
836{
837 if (spec == NULL)
838 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000839
Jim Inghama2664912012-03-07 22:03:04 +0000840 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000841}
842
Chris Lattner24943d22010-06-08 16:52:24 +0000843void
844Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
845{
846 if (thread_plan_sp)
847 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000848 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
849 if (!thread_plan_sp->GetThreadPlanTracer())
850 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000851 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000852
Chris Lattner24943d22010-06-08 16:52:24 +0000853 thread_plan_sp->DidPush();
854
Greg Claytone005f2c2010-11-06 01:53:30 +0000855 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000856 if (log)
857 {
858 StreamString s;
859 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000860 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000861 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000862 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000863 }
864 }
865}
866
867void
868Thread::PopPlan ()
869{
Greg Claytone005f2c2010-11-06 01:53:30 +0000870 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000871
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000872 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000873 return;
874 else
875 {
876 ThreadPlanSP &plan = m_plan_stack.back();
877 if (log)
878 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000879 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000880 }
881 m_completed_plan_stack.push_back (plan);
882 plan->WillPop();
883 m_plan_stack.pop_back();
884 }
885}
886
887void
888Thread::DiscardPlan ()
889{
890 if (m_plan_stack.size() > 1)
891 {
892 ThreadPlanSP &plan = m_plan_stack.back();
893 m_discarded_plan_stack.push_back (plan);
894 plan->WillPop();
895 m_plan_stack.pop_back();
896 }
897}
898
899ThreadPlan *
900Thread::GetCurrentPlan ()
901{
Jim Ingham2f41d272012-04-19 00:17:05 +0000902 // There will always be at least the base plan. If somebody is mucking with a
903 // thread with an empty plan stack, we should assert right away.
904 assert (!m_plan_stack.empty());
905
906 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000907}
908
909ThreadPlanSP
910Thread::GetCompletedPlan ()
911{
912 ThreadPlanSP empty_plan_sp;
913 if (!m_completed_plan_stack.empty())
914 {
915 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
916 {
917 ThreadPlanSP completed_plan_sp;
918 completed_plan_sp = m_completed_plan_stack[i];
919 if (!completed_plan_sp->GetPrivate ())
920 return completed_plan_sp;
921 }
922 }
923 return empty_plan_sp;
924}
925
Jim Ingham1586d972011-12-17 01:35:57 +0000926ValueObjectSP
927Thread::GetReturnValueObject ()
928{
929 if (!m_completed_plan_stack.empty())
930 {
931 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
932 {
933 ValueObjectSP return_valobj_sp;
934 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
935 if (return_valobj_sp)
936 return return_valobj_sp;
937 }
938 }
939 return ValueObjectSP();
940}
941
Chris Lattner24943d22010-06-08 16:52:24 +0000942bool
943Thread::IsThreadPlanDone (ThreadPlan *plan)
944{
Chris Lattner24943d22010-06-08 16:52:24 +0000945 if (!m_completed_plan_stack.empty())
946 {
947 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
948 {
949 if (m_completed_plan_stack[i].get() == plan)
950 return true;
951 }
952 }
953 return false;
954}
955
956bool
957Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
958{
Chris Lattner24943d22010-06-08 16:52:24 +0000959 if (!m_discarded_plan_stack.empty())
960 {
961 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
962 {
963 if (m_discarded_plan_stack[i].get() == plan)
964 return true;
965 }
966 }
967 return false;
968}
969
970ThreadPlan *
971Thread::GetPreviousPlan (ThreadPlan *current_plan)
972{
973 if (current_plan == NULL)
974 return NULL;
975
976 int stack_size = m_completed_plan_stack.size();
977 for (int i = stack_size - 1; i > 0; i--)
978 {
979 if (current_plan == m_completed_plan_stack[i].get())
980 return m_completed_plan_stack[i-1].get();
981 }
982
983 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
984 {
Chris Lattner24943d22010-06-08 16:52:24 +0000985 if (m_plan_stack.size() > 0)
986 return m_plan_stack.back().get();
987 else
988 return NULL;
989 }
990
991 stack_size = m_plan_stack.size();
992 for (int i = stack_size - 1; i > 0; i--)
993 {
994 if (current_plan == m_plan_stack[i].get())
995 return m_plan_stack[i-1].get();
996 }
997 return NULL;
998}
999
1000void
1001Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1002{
1003 if (abort_other_plans)
1004 DiscardThreadPlans(true);
1005
1006 PushPlan (thread_plan_sp);
1007}
1008
Jim Ingham745ac7a2010-11-11 19:26:09 +00001009
1010void
1011Thread::EnableTracer (bool value, bool single_stepping)
1012{
1013 int stack_size = m_plan_stack.size();
1014 for (int i = 0; i < stack_size; i++)
1015 {
1016 if (m_plan_stack[i]->GetThreadPlanTracer())
1017 {
1018 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1019 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1020 }
1021 }
1022}
1023
1024void
1025Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1026{
1027 int stack_size = m_plan_stack.size();
1028 for (int i = 0; i < stack_size; i++)
1029 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1030}
1031
Chris Lattner24943d22010-06-08 16:52:24 +00001032void
Jim Inghamea9d4262010-11-05 19:25:48 +00001033Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1034{
Jim Ingham88e3de22012-05-03 21:19:36 +00001035 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1036}
1037
1038void
1039Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1040{
Greg Claytone005f2c2010-11-06 01:53:30 +00001041 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +00001042 if (log)
1043 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001044 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 +00001045 }
1046
1047 int stack_size = m_plan_stack.size();
1048
1049 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1050 // stack, and if so discard up to and including it.
1051
Jim Ingham88e3de22012-05-03 21:19:36 +00001052 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +00001053 {
1054 for (int i = stack_size - 1; i > 0; i--)
1055 DiscardPlan();
1056 }
1057 else
1058 {
1059 bool found_it = false;
1060 for (int i = stack_size - 1; i > 0; i--)
1061 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001062 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001063 found_it = true;
1064 }
1065 if (found_it)
1066 {
1067 bool last_one = false;
1068 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1069 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001070 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001071 last_one = true;
1072 DiscardPlan();
1073 }
1074 }
1075 }
1076 return;
1077}
1078
1079void
Chris Lattner24943d22010-06-08 16:52:24 +00001080Thread::DiscardThreadPlans(bool force)
1081{
Greg Claytone005f2c2010-11-06 01:53:30 +00001082 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001083 if (log)
1084 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001085 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +00001086 }
1087
1088 if (force)
1089 {
1090 int stack_size = m_plan_stack.size();
1091 for (int i = stack_size - 1; i > 0; i--)
1092 {
1093 DiscardPlan();
1094 }
1095 return;
1096 }
1097
1098 while (1)
1099 {
1100
1101 int master_plan_idx;
Jim Inghamf1dbb712012-09-11 00:09:25 +00001102 bool discard = true;
Chris Lattner24943d22010-06-08 16:52:24 +00001103
1104 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1105 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1106 {
1107 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1108 {
1109 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1110 break;
1111 }
1112 }
1113
1114 if (discard)
1115 {
1116 // First pop all the dependent plans:
1117 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1118 {
1119
1120 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1121 // for the plan leaves it in a state that it is safe to pop the plan
1122 // with no more notice?
1123 DiscardPlan();
1124 }
1125
1126 // Now discard the master plan itself.
1127 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1128 // discard it's dependent plans, but not it...
1129 if (master_plan_idx > 0)
1130 {
1131 DiscardPlan();
1132 }
1133 }
1134 else
1135 {
1136 // If the master plan doesn't want to get discarded, then we're done.
1137 break;
1138 }
1139
1140 }
Chris Lattner24943d22010-06-08 16:52:24 +00001141}
1142
Jim Ingham2bcbaf62012-04-09 22:37:39 +00001143bool
1144Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1145{
1146 if (plan_ptr->IsBasePlan())
1147 return true;
1148 else if (m_plan_stack.size() == 0)
1149 return false;
1150 else
1151 return m_plan_stack[0].get() == plan_ptr;
1152}
1153
Chris Lattner24943d22010-06-08 16:52:24 +00001154ThreadPlan *
1155Thread::QueueFundamentalPlan (bool abort_other_plans)
1156{
1157 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1158 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1159 return thread_plan_sp.get();
1160}
1161
1162ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001163Thread::QueueThreadPlanForStepSingleInstruction
1164(
1165 bool step_over,
1166 bool abort_other_plans,
1167 bool stop_other_threads
1168)
Chris Lattner24943d22010-06-08 16:52:24 +00001169{
1170 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1171 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1172 return thread_plan_sp.get();
1173}
1174
1175ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001176Thread::QueueThreadPlanForStepRange
1177(
1178 bool abort_other_plans,
1179 StepType type,
1180 const AddressRange &range,
1181 const SymbolContext &addr_context,
1182 lldb::RunMode stop_other_threads,
1183 bool avoid_code_without_debug_info
1184)
Chris Lattner24943d22010-06-08 16:52:24 +00001185{
1186 ThreadPlanSP thread_plan_sp;
1187 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001188 {
1189 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1190 if (avoid_code_without_debug_info)
1191 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1192 else
1193 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1194 thread_plan_sp.reset (plan);
1195 }
Chris Lattner24943d22010-06-08 16:52:24 +00001196 else
1197 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1198
1199 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1200 return thread_plan_sp.get();
1201}
1202
1203
1204ThreadPlan *
1205Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1206{
1207 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1208 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1209 return thread_plan_sp.get();
1210}
1211
1212ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001213Thread::QueueThreadPlanForStepOut
1214(
1215 bool abort_other_plans,
1216 SymbolContext *addr_context,
1217 bool first_insn,
1218 bool stop_other_threads,
1219 Vote stop_vote,
1220 Vote run_vote,
1221 uint32_t frame_idx
1222)
Chris Lattner24943d22010-06-08 16:52:24 +00001223{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001224 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1225 addr_context,
1226 first_insn,
1227 stop_other_threads,
1228 stop_vote,
1229 run_vote,
1230 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001231
1232 if (thread_plan_sp->ValidatePlan(NULL))
1233 {
1234 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1235 return thread_plan_sp.get();
1236 }
1237 else
1238 {
1239 return NULL;
1240 }
Chris Lattner24943d22010-06-08 16:52:24 +00001241}
1242
1243ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001244Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001245{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001246 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001247 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1248 return NULL;
1249
Chris Lattner24943d22010-06-08 16:52:24 +00001250 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1251 return thread_plan_sp.get();
1252}
1253
1254ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001255Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1256 Address& function,
1257 lldb::addr_t arg,
1258 bool stop_other_threads,
1259 bool discard_on_error)
1260{
Jim Ingham016ef882011-12-22 19:12:40 +00001261 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001262 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1263 return thread_plan_sp.get();
1264}
1265
1266ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001267Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1268 Address &target_addr,
1269 bool stop_other_threads)
1270{
1271 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1272 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1273 return thread_plan_sp.get();
1274}
1275
1276ThreadPlan *
1277Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001278 lldb::addr_t *address_list,
1279 size_t num_addresses,
1280 bool stop_other_threads,
1281 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001282{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001283 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001284 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1285 return thread_plan_sp.get();
1286
1287}
1288
1289uint32_t
1290Thread::GetIndexID () const
1291{
1292 return m_index_id;
1293}
1294
1295void
1296Thread::DumpThreadPlans (lldb_private::Stream *s) const
1297{
1298 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001299 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001300 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001301 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 +00001302 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001303 {
Chris Lattner24943d22010-06-08 16:52:24 +00001304 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001305 s->Indent();
1306 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001307 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001308 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001309 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001310 }
1311
Chris Lattner24943d22010-06-08 16:52:24 +00001312 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001313 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001314 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001315 s->Indent();
1316 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1317 for (i = stack_size - 1; i >= 0; i--)
1318 {
1319 s->IndentMore();
1320 s->Indent();
1321 s->Printf ("Element %d: ", i);
1322 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1323 s->EOL();
1324 s->IndentLess();
1325 }
Chris Lattner24943d22010-06-08 16:52:24 +00001326 }
1327
1328 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001329 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001330 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001331 s->Indent();
1332 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1333 for (i = stack_size - 1; i >= 0; i--)
1334 {
1335 s->IndentMore();
1336 s->Indent();
1337 s->Printf ("Element %d: ", i);
1338 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1339 s->EOL();
1340 s->IndentLess();
1341 }
Chris Lattner24943d22010-06-08 16:52:24 +00001342 }
1343
1344}
1345
Greg Clayton289afcb2012-02-18 05:35:26 +00001346TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001347Thread::CalculateTarget ()
1348{
Greg Claytonf4124de2012-02-21 00:09:25 +00001349 TargetSP target_sp;
1350 ProcessSP process_sp(GetProcess());
1351 if (process_sp)
1352 target_sp = process_sp->CalculateTarget();
1353 return target_sp;
1354
Chris Lattner24943d22010-06-08 16:52:24 +00001355}
1356
Greg Clayton289afcb2012-02-18 05:35:26 +00001357ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001358Thread::CalculateProcess ()
1359{
Greg Claytonf4124de2012-02-21 00:09:25 +00001360 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001361}
1362
Greg Clayton289afcb2012-02-18 05:35:26 +00001363ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001364Thread::CalculateThread ()
1365{
Greg Clayton289afcb2012-02-18 05:35:26 +00001366 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001367}
1368
Greg Clayton289afcb2012-02-18 05:35:26 +00001369StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001370Thread::CalculateStackFrame ()
1371{
Greg Clayton289afcb2012-02-18 05:35:26 +00001372 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001373}
1374
1375void
Greg Claytona830adb2010-10-04 01:05:56 +00001376Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001377{
Greg Claytonf4124de2012-02-21 00:09:25 +00001378 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001379}
1380
Greg Clayton782b9cc2010-08-25 00:35:26 +00001381
Greg Clayton2450cb12012-03-29 01:41:38 +00001382StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001383Thread::GetStackFrameList ()
1384{
Greg Clayton2450cb12012-03-29 01:41:38 +00001385 StackFrameListSP frame_list_sp;
1386 Mutex::Locker locker(m_frame_mutex);
1387 if (m_curr_frames_sp)
1388 {
1389 frame_list_sp = m_curr_frames_sp;
1390 }
1391 else
1392 {
1393 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1394 m_curr_frames_sp = frame_list_sp;
1395 }
1396 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001397}
1398
Greg Clayton782b9cc2010-08-25 00:35:26 +00001399void
1400Thread::ClearStackFrames ()
1401{
Greg Clayton2450cb12012-03-29 01:41:38 +00001402 Mutex::Locker locker(m_frame_mutex);
1403
Jim Inghambf97d742012-02-29 03:40:22 +00001404 // Only store away the old "reference" StackFrameList if we got all its frames:
1405 // FIXME: At some point we can try to splice in the frames we have fetched into
1406 // the new frame as we make it, but let's not try that now.
1407 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001408 m_prev_frames_sp.swap (m_curr_frames_sp);
1409 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001410}
1411
1412lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001413Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1414{
Greg Clayton2450cb12012-03-29 01:41:38 +00001415 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001416}
1417
Jim Inghama17a81a2012-09-12 00:40:39 +00001418
1419Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001420Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001421{
1422 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1423 Error return_error;
1424
1425 if (!frame_sp)
1426 {
1427 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%llx.", frame_idx, GetID());
1428 }
1429
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001430 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Inghama17a81a2012-09-12 00:40:39 +00001431}
1432
1433Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001434Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001435{
1436 Error return_error;
1437
1438 if (!frame_sp)
1439 {
1440 return_error.SetErrorString("Can't return to a null frame.");
1441 return return_error;
1442 }
1443
1444 Thread *thread = frame_sp->GetThread().get();
Jim Inghamf59388a2012-09-14 02:14:15 +00001445 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1446 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Inghama17a81a2012-09-12 00:40:39 +00001447
1448 if (return_value_sp)
Jim Ingham46657452012-09-27 01:15:29 +00001449 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001450 lldb::ABISP abi = thread->GetProcess()->GetABI();
1451 if (!abi)
1452 {
1453 return_error.SetErrorString("Could not find ABI to set return value.");
1454 }
Jim Ingham46657452012-09-27 01:15:29 +00001455 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1456
1457 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1458 // Turn that back on when that works.
1459 if (0 && sc.function != NULL)
1460 {
1461 Type *function_type = sc.function->GetType();
1462 if (function_type)
1463 {
1464 clang_type_t return_type = sc.function->GetReturnClangType();
1465 if (return_type)
1466 {
1467 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1468 StreamString s;
1469 ast_type.DumpTypeDescription(&s);
1470 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1471 if (cast_value_sp)
1472 {
1473 cast_value_sp->SetFormat(eFormatHex);
1474 return_value_sp = cast_value_sp;
1475 }
1476 }
1477 }
1478 }
1479
Jim Inghamf59388a2012-09-14 02:14:15 +00001480 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001481 if (!return_error.Success())
1482 return return_error;
1483 }
1484
1485 // Now write the return registers for the chosen frame:
Jim Inghamf59388a2012-09-14 02:14:15 +00001486 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1487 // cook their data
1488 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1489 if (copy_success)
Jim Inghama17a81a2012-09-12 00:40:39 +00001490 {
1491 thread->DiscardThreadPlans(true);
Jim Inghamf59388a2012-09-14 02:14:15 +00001492 thread->ClearStackFrames();
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001493 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1494 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
Jim Inghama17a81a2012-09-12 00:40:39 +00001495 return return_error;
1496 }
1497 else
1498 {
1499 return_error.SetErrorString("Could not reset register values.");
1500 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001501 }
1502}
1503
Chris Lattner24943d22010-06-08 16:52:24 +00001504void
Greg Claytona830adb2010-10-04 01:05:56 +00001505Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001506{
Greg Claytonf4124de2012-02-21 00:09:25 +00001507 ExecutionContext exe_ctx (shared_from_this());
1508 Process *process = exe_ctx.GetProcessPtr();
1509 if (process == NULL)
1510 return;
1511
Greg Clayton567e7f32011-09-22 04:58:26 +00001512 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001513 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001514 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001515 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001516 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001517 if (frame_sp)
1518 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001519 exe_ctx.SetFrameSP(frame_sp);
1520 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001521 }
1522 }
1523
Greg Claytonf4124de2012-02-21 00:09:25 +00001524 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001525 assert (thread_format);
1526 const char *end = NULL;
1527 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001528 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001529 &exe_ctx,
1530 NULL,
1531 strm,
1532 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001533}
1534
Greg Clayton990de7b2010-11-18 23:32:35 +00001535void
Caroline Tice2a456812011-03-10 22:14:10 +00001536Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001537{
Greg Clayton990de7b2010-11-18 23:32:35 +00001538}
Jim Ingham20594b12010-09-08 03:14:33 +00001539
Greg Clayton990de7b2010-11-18 23:32:35 +00001540void
Caroline Tice2a456812011-03-10 22:14:10 +00001541Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001542{
Greg Clayton990de7b2010-11-18 23:32:35 +00001543}
Jim Ingham20594b12010-09-08 03:14:33 +00001544
Jim Inghamccd584d2010-09-23 17:40:12 +00001545lldb::StackFrameSP
1546Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1547{
Greg Clayton2450cb12012-03-29 01:41:38 +00001548 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001549}
Caroline Tice7826c882010-10-26 03:11:13 +00001550
1551const char *
1552Thread::StopReasonAsCString (lldb::StopReason reason)
1553{
1554 switch (reason)
1555 {
1556 case eStopReasonInvalid: return "invalid";
1557 case eStopReasonNone: return "none";
1558 case eStopReasonTrace: return "trace";
1559 case eStopReasonBreakpoint: return "breakpoint";
1560 case eStopReasonWatchpoint: return "watchpoint";
1561 case eStopReasonSignal: return "signal";
1562 case eStopReasonException: return "exception";
1563 case eStopReasonPlanComplete: return "plan complete";
1564 }
1565
1566
1567 static char unknown_state_string[64];
1568 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1569 return unknown_state_string;
1570}
1571
1572const char *
1573Thread::RunModeAsCString (lldb::RunMode mode)
1574{
1575 switch (mode)
1576 {
1577 case eOnlyThisThread: return "only this thread";
1578 case eAllThreads: return "all threads";
1579 case eOnlyDuringStepping: return "only during stepping";
1580 }
1581
1582 static char unknown_state_string[64];
1583 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1584 return unknown_state_string;
1585}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001586
Greg Claytonabe0fed2011-04-18 08:33:37 +00001587size_t
1588Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1589{
Greg Claytonf4124de2012-02-21 00:09:25 +00001590 ExecutionContext exe_ctx (shared_from_this());
1591 Target *target = exe_ctx.GetTargetPtr();
1592 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001593 size_t num_frames_shown = 0;
1594 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001595 bool is_selected = false;
1596 if (process)
1597 {
1598 if (process->GetThreadList().GetSelectedThread().get() == this)
1599 is_selected = true;
1600 }
1601 strm.Printf("%c ", is_selected ? '*' : ' ');
1602 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001603 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001604 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001605 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001606 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001607 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1608 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1609 {
1610 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1611 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001612 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001613 }
1614
1615 DumpUsingSettingsFormat (strm, start_frame);
1616
1617 if (num_frames > 0)
1618 {
1619 strm.IndentMore();
1620
1621 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001622 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001623 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1624 start_frame,
1625 num_frames,
1626 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001627 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001628 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001629 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001630 }
1631 return num_frames_shown;
1632}
1633
1634size_t
1635Thread::GetStackFrameStatus (Stream& strm,
1636 uint32_t first_frame,
1637 uint32_t num_frames,
1638 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001639 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001640{
Greg Clayton2450cb12012-03-29 01:41:38 +00001641 return GetStackFrameList()->GetStatus (strm,
1642 first_frame,
1643 num_frames,
1644 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001645 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001646}
1647
Peter Collingbournee426d852011-06-03 20:40:54 +00001648bool
1649Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1650{
1651 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1652 if (frame_sp)
1653 {
1654 checkpoint.SetStackID(frame_sp->GetStackID());
1655 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1656 }
1657 return false;
1658}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001659
Peter Collingbournee426d852011-06-03 20:40:54 +00001660bool
1661Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1662{
Jim Inghama17a81a2012-09-12 00:40:39 +00001663 return ResetFrameZeroRegisters (checkpoint.GetData());
1664}
1665
1666bool
1667Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1668{
Peter Collingbournee426d852011-06-03 20:40:54 +00001669 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1670 if (frame_sp)
1671 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001672 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbournee426d852011-06-03 20:40:54 +00001673
1674 // Clear out all stack frames as our world just changed.
1675 ClearStackFrames();
1676 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1677
1678 return ret;
1679 }
1680 return false;
1681}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001682
Greg Clayton37f962e2011-08-22 02:49:39 +00001683Unwind *
1684Thread::GetUnwinder ()
1685{
1686 if (m_unwinder_ap.get() == NULL)
1687 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001688 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001689 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1690 switch (machine)
1691 {
1692 case llvm::Triple::x86_64:
1693 case llvm::Triple::x86:
1694 case llvm::Triple::arm:
1695 case llvm::Triple::thumb:
1696 m_unwinder_ap.reset (new UnwindLLDB (*this));
1697 break;
1698
1699 default:
1700 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1701 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1702 break;
1703 }
1704 }
1705 return m_unwinder_ap.get();
1706}
1707
1708
Greg Claytoncf5927e2012-05-18 02:38:05 +00001709void
1710Thread::Flush ()
1711{
1712 ClearStackFrames ();
1713 m_reg_context_sp.reset();
1714}