blob: 44f326d77c3f502b1eb935730185c6a5a69d3730 [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/lldb-private-log.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000013#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000014#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
16#include "lldb/Core/Stream.h"
17#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000018#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000019#include "lldb/Host/Host.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000020#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/DynamicLoader.h"
22#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000023#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000026#include "lldb/Target/StopInfo.h"
Greg Clayton896dff62010-07-23 16:45:51 +000027#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
30#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/ThreadPlanBase.h"
32#include "lldb/Target/ThreadPlanStepInstruction.h"
33#include "lldb/Target/ThreadPlanStepOut.h"
34#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
35#include "lldb/Target/ThreadPlanStepThrough.h"
36#include "lldb/Target/ThreadPlanStepInRange.h"
37#include "lldb/Target/ThreadPlanStepOverRange.h"
38#include "lldb/Target/ThreadPlanRunToAddress.h"
39#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000040#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000041#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000042#include "Plugins/Process/Utility/UnwindLLDB.h"
43#include "UnwindMacOSXFrameBackchain.h"
44
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Greg Clayton67cc0632012-08-22 17:17:09 +000049
50const ThreadPropertiesSP &
51Thread::GetGlobalProperties()
52{
53 static ThreadPropertiesSP g_settings_sp;
54 if (!g_settings_sp)
55 g_settings_sp.reset (new ThreadProperties (true));
56 return g_settings_sp;
57}
58
59static PropertyDefinition
60g_properties[] =
61{
62 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
63 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
64 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
65};
66
67enum {
68 ePropertyStepAvoidRegex,
69 ePropertyEnableThreadTrace
70};
71
72
73class ThreadOptionValueProperties : public OptionValueProperties
74{
75public:
76 ThreadOptionValueProperties (const ConstString &name) :
77 OptionValueProperties (name)
78 {
79 }
80
81 // This constructor is used when creating ThreadOptionValueProperties when it
82 // is part of a new lldb_private::Thread instance. It will copy all current
83 // global property values as needed
84 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000085 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000086 {
87 }
88
89 virtual const Property *
90 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
91 {
92 // When gettings the value for a key from the thread options, we will always
93 // try and grab the setting from the current thread if there is one. Else we just
94 // use the one from this instance.
95 if (exe_ctx)
96 {
97 Thread *thread = exe_ctx->GetThreadPtr();
98 if (thread)
99 {
100 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
101 if (this != instance_properties)
102 return instance_properties->ProtectedGetPropertyAtIndex (idx);
103 }
104 }
105 return ProtectedGetPropertyAtIndex (idx);
106 }
107};
108
109
110
111ThreadProperties::ThreadProperties (bool is_global) :
112 Properties ()
113{
114 if (is_global)
115 {
116 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
117 m_collection_sp->Initialize(g_properties);
118 }
119 else
120 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
121}
122
123ThreadProperties::~ThreadProperties()
124{
125}
126
127const RegularExpression *
128ThreadProperties::GetSymbolsToAvoidRegexp()
129{
130 const uint32_t idx = ePropertyStepAvoidRegex;
131 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
132}
133
134bool
135ThreadProperties::GetTraceEnabledState() const
136{
137 const uint32_t idx = ePropertyEnableThreadTrace;
138 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
139}
140
Jim Ingham4f465cf2012-10-10 18:32:14 +0000141//------------------------------------------------------------------
142// Thread Event Data
143//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000144
Jim Ingham4f465cf2012-10-10 18:32:14 +0000145
146const ConstString &
147Thread::ThreadEventData::GetFlavorString ()
148{
149 static ConstString g_flavor ("Thread::ThreadEventData");
150 return g_flavor;
151}
152
153Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
154 m_thread_sp (thread_sp),
155 m_stack_id ()
156{
157}
158
159Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
160 m_thread_sp (thread_sp),
161 m_stack_id (stack_id)
162{
163}
164
165Thread::ThreadEventData::ThreadEventData () :
166 m_thread_sp (),
167 m_stack_id ()
168{
169}
170
171Thread::ThreadEventData::~ThreadEventData ()
172{
173}
174
175void
176Thread::ThreadEventData::Dump (Stream *s) const
177{
178
179}
180
181const Thread::ThreadEventData *
182Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
183{
184 if (event_ptr)
185 {
186 const EventData *event_data = event_ptr->GetData();
187 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
188 return static_cast <const ThreadEventData *> (event_ptr->GetData());
189 }
190 return NULL;
191}
192
193ThreadSP
194Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
195{
196 ThreadSP thread_sp;
197 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
198 if (event_data)
199 thread_sp = event_data->GetThread();
200 return thread_sp;
201}
202
203StackID
204Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
205{
206 StackID stack_id;
207 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
208 if (event_data)
209 stack_id = event_data->GetStackID();
210 return stack_id;
211}
212
213StackFrameSP
214Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
215{
216 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
217 StackFrameSP frame_sp;
218 if (event_data)
219 {
220 ThreadSP thread_sp = event_data->GetThread();
221 if (thread_sp)
222 {
223 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
224 }
225 }
226 return frame_sp;
227}
228
229//------------------------------------------------------------------
230// Thread class
231//------------------------------------------------------------------
232
233ConstString &
234Thread::GetStaticBroadcasterClass ()
235{
236 static ConstString class_name ("lldb.thread");
237 return class_name;
238}
239
240Thread::Thread (Process &process, lldb::tid_t tid) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000241 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000243 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
244 m_process_wp (process.shared_from_this()),
Greg Claytonf4b47e12010-08-04 01:40:35 +0000245 m_actual_stop_info_sp (),
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000246 m_index_id (process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000249 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000252 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000253 m_curr_frames_sp (),
254 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000256 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000257 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000258 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000259 m_destroy_called (false),
260 m_thread_stop_reason_stop_id (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000262 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000264 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265
Jim Ingham4f465cf2012-10-10 18:32:14 +0000266 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267 QueueFundamentalPlan(true);
268}
269
270
271Thread::~Thread()
272{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000273 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000275 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000276 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
277 assert (m_destroy_called);
278}
279
280void
281Thread::DestroyThread ()
282{
Greg Clayton35a4cc52012-10-29 20:52:08 +0000283 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000284 m_plan_stack.clear();
285 m_discarded_plan_stack.clear();
286 m_completed_plan_stack.clear();
Jim Inghamd8ba4642012-04-10 00:44:25 +0000287 m_actual_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000288 m_reg_context_sp.reset();
289 m_unwinder_ap.reset();
290 Mutex::Locker locker(m_frame_mutex);
291 m_curr_frames_sp.reset();
292 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293}
294
Jim Ingham4f465cf2012-10-10 18:32:14 +0000295void
296Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
297{
298 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
299 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
300}
301
302uint32_t
303Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
304{
305 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
306 if (broadcast)
307 BroadcastSelectedFrameChange(frame->GetStackID());
308 return ret_value;
309}
310
311bool
312Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
313{
314 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
315 if (frame_sp)
316 {
317 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
318 if (broadcast)
319 BroadcastSelectedFrameChange(frame_sp->GetStackID());
320 return true;
321 }
322 else
323 return false;
324}
325
Jim Ingham93208b82013-01-31 21:46:01 +0000326bool
327Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
328{
329 const bool broadcast = true;
330 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
331 if (success)
332 {
333 StackFrameSP frame_sp = GetSelectedFrame();
334 if (frame_sp)
335 {
336 bool already_shown = false;
337 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
338 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
339 {
340 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
341 }
342
343 bool show_frame_info = true;
344 bool show_source = !already_shown;
345 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
346 }
347 return false;
348 }
349 else
350 return false;
351}
352
Jim Ingham4f465cf2012-10-10 18:32:14 +0000353
Jim Inghamb15bfc72010-10-20 00:39:53 +0000354lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000355Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356{
Jim Inghamb15bfc72010-10-20 00:39:53 +0000357 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000358 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham73ca05a2011-12-17 01:35:57 +0000359 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Inghamb15bfc72010-10-20 00:39:53 +0000360 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000361 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000362 ProcessSP process_sp (GetProcess());
363 if (process_sp
364 && m_actual_stop_info_sp
Jim Ingham79c35da2011-08-09 00:32:52 +0000365 && m_actual_stop_info_sp->IsValid()
Greg Clayton1ac04c32012-02-21 00:09:25 +0000366 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham79c35da2011-08-09 00:32:52 +0000367 return m_actual_stop_info_sp;
368 else
369 return GetPrivateStopReason ();
370 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371}
372
Greg Clayton97d5cf02012-09-25 02:40:06 +0000373lldb::StopReason
374Thread::GetStopReason()
375{
376 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
377 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000378 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000379 return eStopReasonNone;
380}
381
382
383
Jim Ingham77787032011-01-20 02:03:18 +0000384void
385Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
386{
387 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham79c35da2011-08-09 00:32:52 +0000388 if (m_actual_stop_info_sp)
389 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000390 ProcessSP process_sp (GetProcess());
391 if (process_sp)
392 m_thread_stop_reason_stop_id = process_sp->GetStopID();
393 else
394 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham77787032011-01-20 02:03:18 +0000395}
396
397void
398Thread::SetStopInfoToNothing()
399{
400 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
401 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
402 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
403}
404
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405bool
406Thread::ThreadStoppedForAReason (void)
407{
Jim Inghamf94e1792012-08-11 00:35:26 +0000408 return (bool) GetPrivateStopReason ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409}
410
Jim Ingham77787032011-01-20 02:03:18 +0000411bool
412Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
413{
414 if (!SaveFrameZeroState(saved_state.register_backup))
415 return false;
416
417 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000418 ProcessSP process_sp (GetProcess());
419 if (process_sp)
420 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000421 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
422
Jim Ingham77787032011-01-20 02:03:18 +0000423 return true;
424}
425
426bool
Jim Ingham8559a352012-11-26 23:52:18 +0000427Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000428{
429 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham8559a352012-11-26 23:52:18 +0000430 return true;
431}
432
433bool
434Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
435{
Jim Ingham0c270682011-01-25 02:47:23 +0000436 if (saved_state.stop_info_sp)
437 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000438 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000439 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000440 return true;
441}
442
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443StateType
444Thread::GetState() const
445{
446 // If any other threads access this we will need a mutex for it
447 Mutex::Locker locker(m_state_mutex);
448 return m_state;
449}
450
451void
452Thread::SetState(StateType state)
453{
454 Mutex::Locker locker(m_state_mutex);
455 m_state = state;
456}
457
458void
459Thread::WillStop()
460{
461 ThreadPlan *current_plan = GetCurrentPlan();
462
463 // FIXME: I may decide to disallow threads with no plans. In which
464 // case this should go to an assert.
465
466 if (!current_plan)
467 return;
468
469 current_plan->WillStop();
470}
471
472void
473Thread::SetupForResume ()
474{
475 if (GetResumeState() != eStateSuspended)
476 {
477
478 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
479 // telling the current plan it will resume, since we might change what the current
480 // plan is.
481
Jim Ingham9026dee2013-03-13 01:52:09 +0000482// StopReason stop_reason = lldb::eStopReasonInvalid;
483// StopInfoSP stop_info_sp = GetStopInfo();
484// if (stop_info_sp.get())
485// stop_reason = stop_info_sp->GetStopReason();
486// if (stop_reason == lldb::eStopReasonBreakpoint)
487 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(GetRegisterContext()->GetPC());
488 if (bp_site_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 {
490 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
491 // special to step over a breakpoint.
Jim Inghamb01e7422010-06-19 04:45:32 +0000492
493 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494
Jim Inghamb01e7422010-06-19 04:45:32 +0000495 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
496 {
497 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
498 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000500 ThreadPlanSP step_bp_plan_sp;
501 step_bp_plan->SetPrivate (true);
502
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 if (GetCurrentPlan()->RunState() != eStateStepping)
504 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000505 step_bp_plan->SetAutoContinue(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000507 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508 QueueThreadPlan (step_bp_plan_sp, false);
509 }
510 }
511 }
512 }
513}
514
515bool
516Thread::WillResume (StateType resume_state)
517{
518 // At this point clear the completed plan stack.
519 m_completed_plan_stack.clear();
520 m_discarded_plan_stack.clear();
521
Greg Clayton97d5cf02012-09-25 02:40:06 +0000522 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000523
524 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
525 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
526 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
527 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000528 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham92087d82012-01-31 23:09:20 +0000529 if (m_thread_stop_reason_stop_id == process_stop_id &&
530 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
531 {
532 StopInfo *stop_info = GetPrivateStopReason().get();
533 if (stop_info)
534 stop_info->WillResume (resume_state);
535 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536
537 // Tell all the plans that we are about to resume in case they need to clear any state.
538 // We distinguish between the plan on the top of the stack and the lower
539 // plans in case a plan needs to do any special business before it runs.
540
541 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham513c6bb2012-09-01 01:02:41 +0000542 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000543
544 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
545 {
546 plan_ptr->WillResume (resume_state, false);
547 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000548
Jim Ingham513c6bb2012-09-01 01:02:41 +0000549 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
550 // In that case, don't reset it here.
551
Jim Ingham5d88a062012-10-16 00:09:33 +0000552 if (need_to_resume && resume_state != eStateSuspended)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000553 {
554 m_actual_stop_info_sp.reset();
555 }
556
557 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558}
559
560void
561Thread::DidResume ()
562{
563 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
564}
565
566bool
567Thread::ShouldStop (Event* event_ptr)
568{
569 ThreadPlan *current_plan = GetCurrentPlan();
570 bool should_stop = true;
571
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000572 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000573
574 if (GetResumeState () == eStateSuspended)
575 {
576 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000577 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000578 __FUNCTION__,
579 GetID ());
Daniel Malead01b2952012-11-29 21:49:15 +0000580// log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000581// __FUNCTION__,
582// GetID (),
583// GetRegisterContext()->GetPC());
584 return false;
585 }
586
587 if (GetTemporaryResumeState () == eStateSuspended)
588 {
589 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000590 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000591 __FUNCTION__,
592 GetID ());
Daniel Malead01b2952012-11-29 21:49:15 +0000593// log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000594// __FUNCTION__,
595// GetID (),
596// GetRegisterContext()->GetPC());
597 return false;
598 }
599
600 if (ThreadStoppedForAReason() == false)
601 {
602 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000603 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since no stop reason)",
Jim Ingham92087d82012-01-31 23:09:20 +0000604 __FUNCTION__,
605 GetID (),
606 GetRegisterContext()->GetPC());
607 return false;
608 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000609
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 if (log)
611 {
Daniel Malead01b2952012-11-29 21:49:15 +0000612 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Jim Ingham92087d82012-01-31 23:09:20 +0000613 __FUNCTION__,
614 GetID (),
615 GetRegisterContext()->GetPC());
Jim Ingham10c4b242011-10-15 00:23:43 +0000616 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000618 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000620 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000622
623 // The top most plan always gets to do the trace log...
624 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000625
626 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
627 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
628 // do any more work on this stop.
629 StopInfoSP private_stop_info (GetPrivateStopReason());
630 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
631 {
632 if (log)
633 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
634 return false;
635 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636
Jim Inghambad39e42012-09-05 21:12:49 +0000637 // If we've already been restarted, don't query the plans since the state they would examine is not current.
638 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
639 return false;
640
641 // Before the plans see the state of the world, calculate the current inlined depth.
642 GetStackFrameList()->CalculateCurrentInlinedDepth();
643
Jim Ingham25f66702011-12-03 01:52:59 +0000644 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
645 // If that plan is still working, then we don't need to do any more work. If the plan that explains
646 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
647 // whether they still need to do more work.
648
649 bool done_processing_current_plan = false;
650
Jim Ingham0161b492013-02-09 01:29:05 +0000651 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000652 {
653 if (current_plan->TracerExplainsStop())
654 {
655 done_processing_current_plan = true;
656 should_stop = false;
657 }
658 else
659 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000660 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000661 // does and let it handle the situation.
662 ThreadPlan *plan_ptr = current_plan;
663 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
664 {
Jim Ingham0161b492013-02-09 01:29:05 +0000665 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000666 {
667 should_stop = plan_ptr->ShouldStop (event_ptr);
668
669 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
670 // and all the plans below it off the stack.
671
672 if (plan_ptr->MischiefManaged())
673 {
Jim Ingham031177e2012-05-11 23:49:49 +0000674 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000675 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000676
677 do
678 {
679 if (should_stop)
680 current_plan->WillStop();
681 PopPlan();
682 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000683 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
684 // Now, if the responsible plan was not "Okay to discard" then we're done,
685 // otherwise we forward this to the next plan in the stack below.
686 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
687 done_processing_current_plan = true;
688 else
689 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000690 }
691 else
692 done_processing_current_plan = true;
693
694 break;
695 }
696
697 }
698 }
699 }
700
701 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000703 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000704
Jim Ingham10c4b242011-10-15 00:23:43 +0000705 if (log)
706 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
707
Jim Ingham0f16e732011-02-08 05:20:59 +0000708 // We're starting from the base plan, so just let it decide;
709 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000711 should_stop = current_plan->ShouldStop (event_ptr);
712 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000713 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000714 }
715 else
716 {
717 // Otherwise, don't let the base plan override what the other plans say to do, since
718 // presumably if there were other plans they would know what to do...
719 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000721 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000723
724 should_stop = current_plan->ShouldStop(event_ptr);
725 if (log)
726 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
727 if (current_plan->MischiefManaged())
728 {
729 if (should_stop)
730 current_plan->WillStop();
731
732 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
733 // Otherwise, see if the plan's parent wants to stop.
734
735 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
736 {
737 PopPlan();
738 break;
739 }
740 else
741 {
742
743 PopPlan();
744
745 current_plan = GetCurrentPlan();
746 if (current_plan == NULL)
747 {
748 break;
749 }
750 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 }
752 else
753 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000754 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 }
757 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000758
Jim Inghamb01e7422010-06-19 04:45:32 +0000759 if (over_ride_stop)
760 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000761
762 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
763 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
764 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
765 // This code clears stale plans off the stack.
766
767 if (should_stop)
768 {
769 ThreadPlan *plan_ptr = GetCurrentPlan();
770 while (!PlanIsBasePlan(plan_ptr))
771 {
772 bool stale = plan_ptr->IsPlanStale ();
773 ThreadPlan *examined_plan = plan_ptr;
774 plan_ptr = GetPreviousPlan (examined_plan);
775
776 if (stale)
777 {
778 if (log)
779 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
780 DiscardThreadPlansUpToPlan(examined_plan);
781 }
782 }
783 }
784
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786
Jim Ingham10c4b242011-10-15 00:23:43 +0000787 if (log)
788 {
789 StreamString s;
790 s.IndentMore();
791 DumpThreadPlans(&s);
792 log->Printf ("Plan stack final state:\n%s", s.GetData());
793 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
794 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 return should_stop;
796}
797
798Vote
799Thread::ShouldReportStop (Event* event_ptr)
800{
801 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000802 StateType temp_thread_state = GetTemporaryResumeState();
803
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000804 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000805
806 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
807 {
808 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000809 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000811 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812
Jim Ingham92087d82012-01-31 23:09:20 +0000813 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
814 {
815 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000816 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Jim Ingham92087d82012-01-31 23:09:20 +0000817 return eVoteNoOpinion;
818 }
819
820 if (!ThreadStoppedForAReason())
821 {
822 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000823 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
Jim Ingham92087d82012-01-31 23:09:20 +0000824 return eVoteNoOpinion;
825 }
826
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000827 if (m_completed_plan_stack.size() > 0)
828 {
829 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000830 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000831 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan\n", GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
833 }
834 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000835 {
Jim Ingham0161b492013-02-09 01:29:05 +0000836 Vote thread_vote = eVoteNoOpinion;
837 ThreadPlan *plan_ptr = GetCurrentPlan();
838 while (1)
839 {
840 if (plan_ptr->PlanExplainsStop(event_ptr))
841 {
842 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
843 break;
844 }
845 if (PlanIsBasePlan(plan_ptr))
846 break;
847 else
848 plan_ptr = GetPreviousPlan(plan_ptr);
849 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000850 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +0000851 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan\n", GetID(), thread_vote);
852
853 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000854 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855}
856
857Vote
858Thread::ShouldReportRun (Event* event_ptr)
859{
860 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000861
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 if (thread_state == eStateSuspended
863 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000864 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000866 }
867
868 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 if (m_completed_plan_stack.size() > 0)
870 {
871 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000872 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000873 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 "): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000874 GetIndexID(),
875 GetID(),
876 m_completed_plan_stack.back()->GetName());
877
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
879 }
880 else
Jim Ingham444586b2011-01-24 06:34:17 +0000881 {
882 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000883 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 "): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000884 GetIndexID(),
885 GetID(),
886 GetCurrentPlan()->GetName());
887
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000889 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890}
891
Jim Ingham1b54c882010-06-16 02:00:15 +0000892bool
893Thread::MatchesSpec (const ThreadSpec *spec)
894{
895 if (spec == NULL)
896 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000897
Jim Ingham3d902922012-03-07 22:03:04 +0000898 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +0000899}
900
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901void
902Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
903{
904 if (thread_plan_sp)
905 {
Jim Ingham06e827c2010-11-11 19:26:09 +0000906 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
907 if (!thread_plan_sp->GetThreadPlanTracer())
908 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +0000909 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +0000910
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 thread_plan_sp->DidPush();
912
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000913 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 if (log)
915 {
916 StreamString s;
917 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Daniel Malead01b2952012-11-29 21:49:15 +0000918 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +0000920 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921 }
922 }
923}
924
925void
926Thread::PopPlan ()
927{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000928 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000930 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 return;
932 else
933 {
934 ThreadPlanSP &plan = m_plan_stack.back();
935 if (log)
936 {
Daniel Malead01b2952012-11-29 21:49:15 +0000937 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 }
939 m_completed_plan_stack.push_back (plan);
940 plan->WillPop();
941 m_plan_stack.pop_back();
942 }
943}
944
945void
946Thread::DiscardPlan ()
947{
948 if (m_plan_stack.size() > 1)
949 {
950 ThreadPlanSP &plan = m_plan_stack.back();
951 m_discarded_plan_stack.push_back (plan);
952 plan->WillPop();
953 m_plan_stack.pop_back();
954 }
955}
956
957ThreadPlan *
958Thread::GetCurrentPlan ()
959{
Jim Inghame1471232012-04-19 00:17:05 +0000960 // There will always be at least the base plan. If somebody is mucking with a
961 // thread with an empty plan stack, we should assert right away.
962 assert (!m_plan_stack.empty());
963
964 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965}
966
967ThreadPlanSP
968Thread::GetCompletedPlan ()
969{
970 ThreadPlanSP empty_plan_sp;
971 if (!m_completed_plan_stack.empty())
972 {
973 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
974 {
975 ThreadPlanSP completed_plan_sp;
976 completed_plan_sp = m_completed_plan_stack[i];
977 if (!completed_plan_sp->GetPrivate ())
978 return completed_plan_sp;
979 }
980 }
981 return empty_plan_sp;
982}
983
Jim Ingham73ca05a2011-12-17 01:35:57 +0000984ValueObjectSP
985Thread::GetReturnValueObject ()
986{
987 if (!m_completed_plan_stack.empty())
988 {
989 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
990 {
991 ValueObjectSP return_valobj_sp;
992 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
993 if (return_valobj_sp)
994 return return_valobj_sp;
995 }
996 }
997 return ValueObjectSP();
998}
999
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000bool
1001Thread::IsThreadPlanDone (ThreadPlan *plan)
1002{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 if (!m_completed_plan_stack.empty())
1004 {
1005 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1006 {
1007 if (m_completed_plan_stack[i].get() == plan)
1008 return true;
1009 }
1010 }
1011 return false;
1012}
1013
1014bool
1015Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1016{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 if (!m_discarded_plan_stack.empty())
1018 {
1019 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1020 {
1021 if (m_discarded_plan_stack[i].get() == plan)
1022 return true;
1023 }
1024 }
1025 return false;
1026}
1027
1028ThreadPlan *
1029Thread::GetPreviousPlan (ThreadPlan *current_plan)
1030{
1031 if (current_plan == NULL)
1032 return NULL;
1033
1034 int stack_size = m_completed_plan_stack.size();
1035 for (int i = stack_size - 1; i > 0; i--)
1036 {
1037 if (current_plan == m_completed_plan_stack[i].get())
1038 return m_completed_plan_stack[i-1].get();
1039 }
1040
1041 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1042 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043 if (m_plan_stack.size() > 0)
1044 return m_plan_stack.back().get();
1045 else
1046 return NULL;
1047 }
1048
1049 stack_size = m_plan_stack.size();
1050 for (int i = stack_size - 1; i > 0; i--)
1051 {
1052 if (current_plan == m_plan_stack[i].get())
1053 return m_plan_stack[i-1].get();
1054 }
1055 return NULL;
1056}
1057
1058void
1059Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1060{
1061 if (abort_other_plans)
1062 DiscardThreadPlans(true);
1063
1064 PushPlan (thread_plan_sp);
1065}
1066
Jim Ingham06e827c2010-11-11 19:26:09 +00001067
1068void
1069Thread::EnableTracer (bool value, bool single_stepping)
1070{
1071 int stack_size = m_plan_stack.size();
1072 for (int i = 0; i < stack_size; i++)
1073 {
1074 if (m_plan_stack[i]->GetThreadPlanTracer())
1075 {
1076 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1077 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1078 }
1079 }
1080}
1081
1082void
1083Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1084{
1085 int stack_size = m_plan_stack.size();
1086 for (int i = 0; i < stack_size; i++)
1087 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1088}
1089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001091Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1092{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001093 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1094}
1095
1096void
1097Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1098{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001099 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001100 if (log)
1101 {
Daniel Malead01b2952012-11-29 21:49:15 +00001102 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", GetID(), up_to_plan_ptr);
Jim Ingham399f1ca2010-11-05 19:25:48 +00001103 }
1104
1105 int stack_size = m_plan_stack.size();
1106
1107 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1108 // stack, and if so discard up to and including it.
1109
Jim Ingham64e7ead2012-05-03 21:19:36 +00001110 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001111 {
1112 for (int i = stack_size - 1; i > 0; i--)
1113 DiscardPlan();
1114 }
1115 else
1116 {
1117 bool found_it = false;
1118 for (int i = stack_size - 1; i > 0; i--)
1119 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001120 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001121 found_it = true;
1122 }
1123 if (found_it)
1124 {
1125 bool last_one = false;
1126 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1127 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001128 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001129 last_one = true;
1130 DiscardPlan();
1131 }
1132 }
1133 }
1134 return;
1135}
1136
1137void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138Thread::DiscardThreadPlans(bool force)
1139{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001140 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 if (log)
1142 {
Daniel Malead01b2952012-11-29 21:49:15 +00001143 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 }
1145
1146 if (force)
1147 {
1148 int stack_size = m_plan_stack.size();
1149 for (int i = stack_size - 1; i > 0; i--)
1150 {
1151 DiscardPlan();
1152 }
1153 return;
1154 }
1155
1156 while (1)
1157 {
1158
1159 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001160 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161
1162 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1163 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1164 {
1165 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1166 {
1167 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1168 break;
1169 }
1170 }
1171
1172 if (discard)
1173 {
1174 // First pop all the dependent plans:
1175 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1176 {
1177
1178 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1179 // for the plan leaves it in a state that it is safe to pop the plan
1180 // with no more notice?
1181 DiscardPlan();
1182 }
1183
1184 // Now discard the master plan itself.
1185 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1186 // discard it's dependent plans, but not it...
1187 if (master_plan_idx > 0)
1188 {
1189 DiscardPlan();
1190 }
1191 }
1192 else
1193 {
1194 // If the master plan doesn't want to get discarded, then we're done.
1195 break;
1196 }
1197
1198 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199}
1200
Jim Inghamcf274f92012-04-09 22:37:39 +00001201bool
1202Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1203{
1204 if (plan_ptr->IsBasePlan())
1205 return true;
1206 else if (m_plan_stack.size() == 0)
1207 return false;
1208 else
1209 return m_plan_stack[0].get() == plan_ptr;
1210}
1211
Jim Ingham93208b82013-01-31 21:46:01 +00001212Error
1213Thread::UnwindInnermostExpression()
1214{
1215 Error error;
1216 int stack_size = m_plan_stack.size();
1217
1218 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1219 // stack, and if so discard up to and including it.
1220
1221 for (int i = stack_size - 1; i > 0; i--)
1222 {
1223 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1224 {
1225 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1226 return error;
1227 }
1228 }
1229 error.SetErrorString("No expressions currently active on this thread");
1230 return error;
1231}
1232
1233
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234ThreadPlan *
1235Thread::QueueFundamentalPlan (bool abort_other_plans)
1236{
1237 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1238 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1239 return thread_plan_sp.get();
1240}
1241
1242ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001243Thread::QueueThreadPlanForStepSingleInstruction
1244(
1245 bool step_over,
1246 bool abort_other_plans,
1247 bool stop_other_threads
1248)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249{
1250 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1251 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1252 return thread_plan_sp.get();
1253}
1254
1255ThreadPlan *
Jim Inghamc6276822012-12-12 19:58:40 +00001256Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001257(
1258 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001259 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001260 const SymbolContext &addr_context,
1261 lldb::RunMode stop_other_threads
1262)
1263{
1264 ThreadPlanSP thread_plan_sp;
1265 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1266
1267 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1268 return thread_plan_sp.get();
1269}
1270
1271ThreadPlan *
1272Thread::QueueThreadPlanForStepInRange
1273(
1274 bool abort_other_plans,
1275 const AddressRange &range,
1276 const SymbolContext &addr_context,
1277 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001278 lldb::RunMode stop_other_threads,
1279 bool avoid_code_without_debug_info
1280)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281{
1282 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001283 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1284 if (avoid_code_without_debug_info)
1285 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286 else
Jim Inghamc6276822012-12-12 19:58:40 +00001287 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1288 if (step_in_target)
1289 plan->SetStepInTarget(step_in_target);
1290 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291
1292 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1293 return thread_plan_sp.get();
1294}
1295
1296
1297ThreadPlan *
1298Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1299{
1300 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1301 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1302 return thread_plan_sp.get();
1303}
1304
1305ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001306Thread::QueueThreadPlanForStepOut
1307(
1308 bool abort_other_plans,
1309 SymbolContext *addr_context,
1310 bool first_insn,
1311 bool stop_other_threads,
1312 Vote stop_vote,
1313 Vote run_vote,
1314 uint32_t frame_idx
1315)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316{
Greg Clayton481cef22011-01-21 06:11:58 +00001317 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1318 addr_context,
1319 first_insn,
1320 stop_other_threads,
1321 stop_vote,
1322 run_vote,
1323 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001324
1325 if (thread_plan_sp->ValidatePlan(NULL))
1326 {
1327 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1328 return thread_plan_sp.get();
1329 }
1330 else
1331 {
1332 return NULL;
1333 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334}
1335
1336ThreadPlan *
Jim Ingham18de2fd2012-05-10 01:35:39 +00001337Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001339 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001340 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1341 return NULL;
1342
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1344 return thread_plan_sp.get();
1345}
1346
1347ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1349 Address& function,
1350 lldb::addr_t arg,
1351 bool stop_other_threads,
Jim Ingham184e9812013-01-15 02:47:48 +00001352 bool unwind_on_error,
1353 bool ignore_breakpoints)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354{
Jim Ingham184e9812013-01-15 02:47:48 +00001355 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1356 function,
1357 ClangASTType(),
1358 arg,
1359 stop_other_threads,
1360 unwind_on_error,
1361 ignore_breakpoints));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001362 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1363 return thread_plan_sp.get();
1364}
1365
1366ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1368 Address &target_addr,
1369 bool stop_other_threads)
1370{
1371 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1372 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1373 return thread_plan_sp.get();
1374}
1375
1376ThreadPlan *
1377Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001378 lldb::addr_t *address_list,
1379 size_t num_addresses,
1380 bool stop_other_threads,
1381 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001382{
Greg Clayton481cef22011-01-21 06:11:58 +00001383 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1385 return thread_plan_sp.get();
1386
1387}
1388
1389uint32_t
1390Thread::GetIndexID () const
1391{
1392 return m_index_id;
1393}
1394
1395void
1396Thread::DumpThreadPlans (lldb_private::Stream *s) const
1397{
1398 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001399 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001400 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001401 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4" PRIx64 ", stack_size = %d\n", GetIndexID(), GetID(), stack_size);
Greg Clayton1346f7e2010-09-03 22:45:01 +00001402 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001405 s->Indent();
1406 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001409 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 }
1411
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001413 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001415 s->Indent();
1416 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1417 for (i = stack_size - 1; i >= 0; i--)
1418 {
1419 s->IndentMore();
1420 s->Indent();
1421 s->Printf ("Element %d: ", i);
1422 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1423 s->EOL();
1424 s->IndentLess();
1425 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426 }
1427
1428 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001429 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001431 s->Indent();
1432 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1433 for (i = stack_size - 1; i >= 0; i--)
1434 {
1435 s->IndentMore();
1436 s->Indent();
1437 s->Printf ("Element %d: ", i);
1438 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1439 s->EOL();
1440 s->IndentLess();
1441 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 }
1443
1444}
1445
Greg Claytond9e416c2012-02-18 05:35:26 +00001446TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447Thread::CalculateTarget ()
1448{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001449 TargetSP target_sp;
1450 ProcessSP process_sp(GetProcess());
1451 if (process_sp)
1452 target_sp = process_sp->CalculateTarget();
1453 return target_sp;
1454
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455}
1456
Greg Claytond9e416c2012-02-18 05:35:26 +00001457ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458Thread::CalculateProcess ()
1459{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001460 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461}
1462
Greg Claytond9e416c2012-02-18 05:35:26 +00001463ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464Thread::CalculateThread ()
1465{
Greg Claytond9e416c2012-02-18 05:35:26 +00001466 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467}
1468
Greg Claytond9e416c2012-02-18 05:35:26 +00001469StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470Thread::CalculateStackFrame ()
1471{
Greg Claytond9e416c2012-02-18 05:35:26 +00001472 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001473}
1474
1475void
Greg Clayton0603aa92010-10-04 01:05:56 +00001476Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001478 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479}
1480
Greg Clayton12daf9462010-08-25 00:35:26 +00001481
Greg Clayton39d0ab32012-03-29 01:41:38 +00001482StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001483Thread::GetStackFrameList ()
1484{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001485 StackFrameListSP frame_list_sp;
1486 Mutex::Locker locker(m_frame_mutex);
1487 if (m_curr_frames_sp)
1488 {
1489 frame_list_sp = m_curr_frames_sp;
1490 }
1491 else
1492 {
1493 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1494 m_curr_frames_sp = frame_list_sp;
1495 }
1496 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001497}
1498
Greg Clayton12daf9462010-08-25 00:35:26 +00001499void
1500Thread::ClearStackFrames ()
1501{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001502 Mutex::Locker locker(m_frame_mutex);
1503
Jim Inghamb0c72a52012-02-29 03:40:22 +00001504 // Only store away the old "reference" StackFrameList if we got all its frames:
1505 // FIXME: At some point we can try to splice in the frames we have fetched into
1506 // the new frame as we make it, but let's not try that now.
1507 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001508 m_prev_frames_sp.swap (m_curr_frames_sp);
1509 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001510}
1511
1512lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001513Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1514{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001515 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001516}
1517
Jim Ingham44137582012-09-12 00:40:39 +00001518
1519Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001520Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001521{
1522 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1523 Error return_error;
1524
1525 if (!frame_sp)
1526 {
Daniel Malead01b2952012-11-29 21:49:15 +00001527 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001528 }
1529
Jim Ingham4f465cf2012-10-10 18:32:14 +00001530 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001531}
1532
1533Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001534Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001535{
1536 Error return_error;
1537
1538 if (!frame_sp)
1539 {
1540 return_error.SetErrorString("Can't return to a null frame.");
1541 return return_error;
1542 }
1543
1544 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001545 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1546 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001547 if (!older_frame_sp)
1548 {
1549 return_error.SetErrorString("No older frame to return to.");
1550 return return_error;
1551 }
Jim Ingham44137582012-09-12 00:40:39 +00001552
1553 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001554 {
Jim Ingham44137582012-09-12 00:40:39 +00001555 lldb::ABISP abi = thread->GetProcess()->GetABI();
1556 if (!abi)
1557 {
1558 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001559 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001560 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001561 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1562
1563 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1564 // Turn that back on when that works.
1565 if (0 && sc.function != NULL)
1566 {
1567 Type *function_type = sc.function->GetType();
1568 if (function_type)
1569 {
1570 clang_type_t return_type = sc.function->GetReturnClangType();
1571 if (return_type)
1572 {
1573 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1574 StreamString s;
1575 ast_type.DumpTypeDescription(&s);
1576 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1577 if (cast_value_sp)
1578 {
1579 cast_value_sp->SetFormat(eFormatHex);
1580 return_value_sp = cast_value_sp;
1581 }
1582 }
1583 }
1584 }
1585
Jim Inghamcb640dd2012-09-14 02:14:15 +00001586 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001587 if (!return_error.Success())
1588 return return_error;
1589 }
1590
1591 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001592 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001593 // cook their data
1594
1595 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1596 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001597 {
Jim Ingham93208b82013-01-31 21:46:01 +00001598 bool copy_success = youngest_frame_sp->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1599 if (copy_success)
1600 {
1601 thread->DiscardThreadPlans(true);
1602 thread->ClearStackFrames();
1603 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1604 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
Jim Ingham93208b82013-01-31 21:46:01 +00001605 }
1606 else
1607 {
1608 return_error.SetErrorString("Could not reset register values.");
Jim Ingham93208b82013-01-31 21:46:01 +00001609 }
Jim Ingham44137582012-09-12 00:40:39 +00001610 }
1611 else
1612 {
Jim Ingham93208b82013-01-31 21:46:01 +00001613 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001614 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001615 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001616}
1617
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001618void
Greg Clayton0603aa92010-10-04 01:05:56 +00001619Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001620{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001621 ExecutionContext exe_ctx (shared_from_this());
1622 Process *process = exe_ctx.GetProcessPtr();
1623 if (process == NULL)
1624 return;
1625
Greg Claytonc14ee322011-09-22 04:58:26 +00001626 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001627 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001628 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001630 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001631 if (frame_sp)
1632 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001633 exe_ctx.SetFrameSP(frame_sp);
1634 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 }
1636 }
1637
Greg Clayton1ac04c32012-02-21 00:09:25 +00001638 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001639 assert (thread_format);
1640 const char *end = NULL;
1641 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001642 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001643 &exe_ctx,
1644 NULL,
1645 strm,
1646 &end);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647}
1648
Greg Clayton99d0faf2010-11-18 23:32:35 +00001649void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001650Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001651{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001652}
Jim Inghamee8aea12010-09-08 03:14:33 +00001653
Greg Clayton99d0faf2010-11-18 23:32:35 +00001654void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001655Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001656{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001657}
Jim Inghamee8aea12010-09-08 03:14:33 +00001658
Jim Inghame4284b72010-09-23 17:40:12 +00001659lldb::StackFrameSP
1660Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1661{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001662 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001663}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001664
1665const char *
1666Thread::StopReasonAsCString (lldb::StopReason reason)
1667{
1668 switch (reason)
1669 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001670 case eStopReasonInvalid: return "invalid";
1671 case eStopReasonNone: return "none";
1672 case eStopReasonTrace: return "trace";
1673 case eStopReasonBreakpoint: return "breakpoint";
1674 case eStopReasonWatchpoint: return "watchpoint";
1675 case eStopReasonSignal: return "signal";
1676 case eStopReasonException: return "exception";
1677 case eStopReasonExec: return "exec";
1678 case eStopReasonPlanComplete: return "plan complete";
1679 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001680 }
1681
1682
1683 static char unknown_state_string[64];
1684 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1685 return unknown_state_string;
1686}
1687
1688const char *
1689Thread::RunModeAsCString (lldb::RunMode mode)
1690{
1691 switch (mode)
1692 {
1693 case eOnlyThisThread: return "only this thread";
1694 case eAllThreads: return "all threads";
1695 case eOnlyDuringStepping: return "only during stepping";
1696 }
1697
1698 static char unknown_state_string[64];
1699 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1700 return unknown_state_string;
1701}
Jim Ingham06e827c2010-11-11 19:26:09 +00001702
Greg Clayton7260f622011-04-18 08:33:37 +00001703size_t
1704Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1705{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001706 ExecutionContext exe_ctx (shared_from_this());
1707 Target *target = exe_ctx.GetTargetPtr();
1708 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001709 size_t num_frames_shown = 0;
1710 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001711 bool is_selected = false;
1712 if (process)
1713 {
1714 if (process->GetThreadList().GetSelectedThread().get() == this)
1715 is_selected = true;
1716 }
1717 strm.Printf("%c ", is_selected ? '*' : ' ');
1718 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001719 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001720 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001721 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001722 {
Jim Inghame610d642011-08-16 00:07:28 +00001723 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1724 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1725 {
1726 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1727 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001728 }
Greg Clayton7260f622011-04-18 08:33:37 +00001729 }
1730
1731 DumpUsingSettingsFormat (strm, start_frame);
1732
1733 if (num_frames > 0)
1734 {
1735 strm.IndentMore();
1736
1737 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001738 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001739 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1740 start_frame,
1741 num_frames,
1742 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001743 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001744 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001745 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001746 }
1747 return num_frames_shown;
1748}
1749
1750size_t
1751Thread::GetStackFrameStatus (Stream& strm,
1752 uint32_t first_frame,
1753 uint32_t num_frames,
1754 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001755 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001756{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001757 return GetStackFrameList()->GetStatus (strm,
1758 first_frame,
1759 num_frames,
1760 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001761 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001762}
1763
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001764bool
1765Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1766{
1767 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1768 if (frame_sp)
1769 {
1770 checkpoint.SetStackID(frame_sp->GetStackID());
1771 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1772 }
1773 return false;
1774}
Greg Clayton7260f622011-04-18 08:33:37 +00001775
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001776bool
1777Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1778{
Jim Ingham44137582012-09-12 00:40:39 +00001779 return ResetFrameZeroRegisters (checkpoint.GetData());
1780}
1781
1782bool
1783Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1784{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001785 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1786 if (frame_sp)
1787 {
Jim Ingham44137582012-09-12 00:40:39 +00001788 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001789
1790 // Clear out all stack frames as our world just changed.
1791 ClearStackFrames();
1792 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
Jason Molendad525d072012-11-14 04:26:02 +00001793 if (m_unwinder_ap.get())
1794 m_unwinder_ap->Clear();
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001795
1796 return ret;
1797 }
1798 return false;
1799}
Greg Clayton7260f622011-04-18 08:33:37 +00001800
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001801Unwind *
1802Thread::GetUnwinder ()
1803{
1804 if (m_unwinder_ap.get() == NULL)
1805 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001806 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001807 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1808 switch (machine)
1809 {
1810 case llvm::Triple::x86_64:
1811 case llvm::Triple::x86:
1812 case llvm::Triple::arm:
1813 case llvm::Triple::thumb:
1814 m_unwinder_ap.reset (new UnwindLLDB (*this));
1815 break;
1816
1817 default:
1818 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1819 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1820 break;
1821 }
1822 }
1823 return m_unwinder_ap.get();
1824}
1825
1826
Greg Claytonfa559e52012-05-18 02:38:05 +00001827void
1828Thread::Flush ()
1829{
1830 ClearStackFrames ();
1831 m_reg_context_sp.reset();
1832}
Jim Ingham5d88a062012-10-16 00:09:33 +00001833
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00001834bool
Jim Ingham5d88a062012-10-16 00:09:33 +00001835Thread::IsStillAtLastBreakpointHit ()
1836{
1837 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1838 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1839 // multithreaded programs.
1840 if (m_actual_stop_info_sp) {
1841 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason();
1842 if (stop_reason == lldb::eStopReasonBreakpoint) {
1843 uint64_t value = m_actual_stop_info_sp->GetValue();
1844 lldb::addr_t pc = GetRegisterContext()->GetPC();
1845 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1846 if (bp_site_sp && value == bp_site_sp->GetID())
1847 return true;
1848 }
1849 }
1850 return false;
1851}