blob: 378d0266d5ace77b44a5cb0416deeb152392885a [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 Clayton5160ce52013-03-27 23:08:40 +0000262 Log *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 Clayton5160ce52013-03-27 23:08:40 +0000273 Log *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
Greg Clayton1afa68e2013-04-02 20:32:37 +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 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
488 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000490 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
491 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000492 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000493 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
494 // special to step over a breakpoint.
495
496 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000497
Greg Clayton1afa68e2013-04-02 20:32:37 +0000498 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
499 {
500 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
501 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000503 ThreadPlanSP step_bp_plan_sp;
504 step_bp_plan->SetPrivate (true);
505
506 if (GetCurrentPlan()->RunState() != eStateStepping)
507 {
508 step_bp_plan->SetAutoContinue(true);
509 }
510 step_bp_plan_sp.reset (step_bp_plan);
511 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 }
514 }
515 }
516 }
517}
518
519bool
520Thread::WillResume (StateType resume_state)
521{
522 // At this point clear the completed plan stack.
523 m_completed_plan_stack.clear();
524 m_discarded_plan_stack.clear();
525
Greg Clayton97d5cf02012-09-25 02:40:06 +0000526 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000527
528 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
529 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
530 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
531 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000532 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham92087d82012-01-31 23:09:20 +0000533 if (m_thread_stop_reason_stop_id == process_stop_id &&
534 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
535 {
536 StopInfo *stop_info = GetPrivateStopReason().get();
537 if (stop_info)
538 stop_info->WillResume (resume_state);
539 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540
541 // Tell all the plans that we are about to resume in case they need to clear any state.
542 // We distinguish between the plan on the top of the stack and the lower
543 // plans in case a plan needs to do any special business before it runs.
544
545 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham513c6bb2012-09-01 01:02:41 +0000546 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547
548 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
549 {
550 plan_ptr->WillResume (resume_state, false);
551 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000552
Jim Ingham513c6bb2012-09-01 01:02:41 +0000553 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
554 // In that case, don't reset it here.
555
Jim Ingham5d88a062012-10-16 00:09:33 +0000556 if (need_to_resume && resume_state != eStateSuspended)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000557 {
558 m_actual_stop_info_sp.reset();
559 }
560
561 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562}
563
564void
565Thread::DidResume ()
566{
567 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
568}
569
570bool
571Thread::ShouldStop (Event* event_ptr)
572{
573 ThreadPlan *current_plan = GetCurrentPlan();
574 bool should_stop = true;
575
Greg Clayton5160ce52013-03-27 23:08:40 +0000576 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000577
578 if (GetResumeState () == eStateSuspended)
579 {
580 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000581 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 +0000582 __FUNCTION__,
583 GetID ());
Jim Ingham92087d82012-01-31 23:09:20 +0000584 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 ());
Jim Ingham92087d82012-01-31 23:09:20 +0000593 return false;
594 }
595
596 if (ThreadStoppedForAReason() == false)
597 {
598 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000599 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 +0000600 __FUNCTION__,
601 GetID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000602 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000603 return false;
604 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000605
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 if (log)
607 {
Daniel Malead01b2952012-11-29 21:49:15 +0000608 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Jim Ingham92087d82012-01-31 23:09:20 +0000609 __FUNCTION__,
610 GetID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000611 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000612 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000614 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000616 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000618
619 // The top most plan always gets to do the trace log...
620 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000621
622 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
623 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
624 // do any more work on this stop.
625 StopInfoSP private_stop_info (GetPrivateStopReason());
626 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
627 {
628 if (log)
629 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
630 return false;
631 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632
Jim Inghambad39e42012-09-05 21:12:49 +0000633 // If we've already been restarted, don't query the plans since the state they would examine is not current.
634 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
635 return false;
636
637 // Before the plans see the state of the world, calculate the current inlined depth.
638 GetStackFrameList()->CalculateCurrentInlinedDepth();
639
Jim Ingham25f66702011-12-03 01:52:59 +0000640 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
641 // If that plan is still working, then we don't need to do any more work. If the plan that explains
642 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
643 // whether they still need to do more work.
644
645 bool done_processing_current_plan = false;
646
Jim Ingham0161b492013-02-09 01:29:05 +0000647 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000648 {
649 if (current_plan->TracerExplainsStop())
650 {
651 done_processing_current_plan = true;
652 should_stop = false;
653 }
654 else
655 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000656 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000657 // does and let it handle the situation.
658 ThreadPlan *plan_ptr = current_plan;
659 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
660 {
Jim Ingham0161b492013-02-09 01:29:05 +0000661 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000662 {
663 should_stop = plan_ptr->ShouldStop (event_ptr);
664
665 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
666 // and all the plans below it off the stack.
667
668 if (plan_ptr->MischiefManaged())
669 {
Jim Ingham031177e2012-05-11 23:49:49 +0000670 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000671 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000672
673 do
674 {
675 if (should_stop)
676 current_plan->WillStop();
677 PopPlan();
678 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000679 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
680 // Now, if the responsible plan was not "Okay to discard" then we're done,
681 // otherwise we forward this to the next plan in the stack below.
682 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
683 done_processing_current_plan = true;
684 else
685 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000686 }
687 else
688 done_processing_current_plan = true;
689
690 break;
691 }
692
693 }
694 }
695 }
696
697 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000699 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000700
Jim Ingham10c4b242011-10-15 00:23:43 +0000701 if (log)
702 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
703
Jim Ingham0f16e732011-02-08 05:20:59 +0000704 // We're starting from the base plan, so just let it decide;
705 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000707 should_stop = current_plan->ShouldStop (event_ptr);
708 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000709 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000710 }
711 else
712 {
713 // Otherwise, don't let the base plan override what the other plans say to do, since
714 // presumably if there were other plans they would know what to do...
715 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000717 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000719
720 should_stop = current_plan->ShouldStop(event_ptr);
721 if (log)
722 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
723 if (current_plan->MischiefManaged())
724 {
725 if (should_stop)
726 current_plan->WillStop();
727
728 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
729 // Otherwise, see if the plan's parent wants to stop.
730
731 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
732 {
733 PopPlan();
734 break;
735 }
736 else
737 {
738
739 PopPlan();
740
741 current_plan = GetCurrentPlan();
742 if (current_plan == NULL)
743 {
744 break;
745 }
746 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 }
748 else
749 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000750 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 }
753 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000754
Jim Inghamb01e7422010-06-19 04:45:32 +0000755 if (over_ride_stop)
756 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000757
758 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
759 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
760 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
761 // This code clears stale plans off the stack.
762
763 if (should_stop)
764 {
765 ThreadPlan *plan_ptr = GetCurrentPlan();
766 while (!PlanIsBasePlan(plan_ptr))
767 {
768 bool stale = plan_ptr->IsPlanStale ();
769 ThreadPlan *examined_plan = plan_ptr;
770 plan_ptr = GetPreviousPlan (examined_plan);
771
772 if (stale)
773 {
774 if (log)
775 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
776 DiscardThreadPlansUpToPlan(examined_plan);
777 }
778 }
779 }
780
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782
Jim Ingham10c4b242011-10-15 00:23:43 +0000783 if (log)
784 {
785 StreamString s;
786 s.IndentMore();
787 DumpThreadPlans(&s);
788 log->Printf ("Plan stack final state:\n%s", s.GetData());
789 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
790 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 return should_stop;
792}
793
794Vote
795Thread::ShouldReportStop (Event* event_ptr)
796{
797 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000798 StateType temp_thread_state = GetTemporaryResumeState();
799
Greg Clayton5160ce52013-03-27 23:08:40 +0000800 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000801
802 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
803 {
804 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000805 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 +0000806 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000807 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808
Jim Ingham92087d82012-01-31 23:09:20 +0000809 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
810 {
811 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000812 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 +0000813 return eVoteNoOpinion;
814 }
815
816 if (!ThreadStoppedForAReason())
817 {
818 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000819 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 +0000820 return eVoteNoOpinion;
821 }
822
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823 if (m_completed_plan_stack.size() > 0)
824 {
825 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000826 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000827 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 +0000828 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
829 }
830 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000831 {
Jim Ingham0161b492013-02-09 01:29:05 +0000832 Vote thread_vote = eVoteNoOpinion;
833 ThreadPlan *plan_ptr = GetCurrentPlan();
834 while (1)
835 {
836 if (plan_ptr->PlanExplainsStop(event_ptr))
837 {
838 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
839 break;
840 }
841 if (PlanIsBasePlan(plan_ptr))
842 break;
843 else
844 plan_ptr = GetPreviousPlan(plan_ptr);
845 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000846 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +0000847 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan\n", GetID(), thread_vote);
848
849 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000850 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851}
852
853Vote
854Thread::ShouldReportRun (Event* event_ptr)
855{
856 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000857
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858 if (thread_state == eStateSuspended
859 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000860 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000862 }
863
Greg Clayton5160ce52013-03-27 23:08:40 +0000864 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 if (m_completed_plan_stack.size() > 0)
866 {
867 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000868 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000869 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 +0000870 GetIndexID(),
871 GetID(),
872 m_completed_plan_stack.back()->GetName());
873
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
875 }
876 else
Jim Ingham444586b2011-01-24 06:34:17 +0000877 {
878 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000879 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 +0000880 GetIndexID(),
881 GetID(),
882 GetCurrentPlan()->GetName());
883
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000885 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886}
887
Jim Ingham1b54c882010-06-16 02:00:15 +0000888bool
889Thread::MatchesSpec (const ThreadSpec *spec)
890{
891 if (spec == NULL)
892 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000893
Jim Ingham3d902922012-03-07 22:03:04 +0000894 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +0000895}
896
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897void
898Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
899{
900 if (thread_plan_sp)
901 {
Jim Ingham06e827c2010-11-11 19:26:09 +0000902 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
903 if (!thread_plan_sp->GetThreadPlanTracer())
904 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +0000905 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +0000906
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 thread_plan_sp->DidPush();
908
Greg Clayton5160ce52013-03-27 23:08:40 +0000909 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910 if (log)
911 {
912 StreamString s;
913 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Daniel Malead01b2952012-11-29 21:49:15 +0000914 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +0000916 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917 }
918 }
919}
920
921void
922Thread::PopPlan ()
923{
Greg Clayton5160ce52013-03-27 23:08:40 +0000924 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000926 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 return;
928 else
929 {
930 ThreadPlanSP &plan = m_plan_stack.back();
931 if (log)
932 {
Daniel Malead01b2952012-11-29 21:49:15 +0000933 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934 }
935 m_completed_plan_stack.push_back (plan);
936 plan->WillPop();
937 m_plan_stack.pop_back();
938 }
939}
940
941void
942Thread::DiscardPlan ()
943{
944 if (m_plan_stack.size() > 1)
945 {
946 ThreadPlanSP &plan = m_plan_stack.back();
947 m_discarded_plan_stack.push_back (plan);
948 plan->WillPop();
949 m_plan_stack.pop_back();
950 }
951}
952
953ThreadPlan *
954Thread::GetCurrentPlan ()
955{
Jim Inghame1471232012-04-19 00:17:05 +0000956 // There will always be at least the base plan. If somebody is mucking with a
957 // thread with an empty plan stack, we should assert right away.
958 assert (!m_plan_stack.empty());
959
960 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961}
962
963ThreadPlanSP
964Thread::GetCompletedPlan ()
965{
966 ThreadPlanSP empty_plan_sp;
967 if (!m_completed_plan_stack.empty())
968 {
969 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
970 {
971 ThreadPlanSP completed_plan_sp;
972 completed_plan_sp = m_completed_plan_stack[i];
973 if (!completed_plan_sp->GetPrivate ())
974 return completed_plan_sp;
975 }
976 }
977 return empty_plan_sp;
978}
979
Jim Ingham73ca05a2011-12-17 01:35:57 +0000980ValueObjectSP
981Thread::GetReturnValueObject ()
982{
983 if (!m_completed_plan_stack.empty())
984 {
985 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
986 {
987 ValueObjectSP return_valobj_sp;
988 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
989 if (return_valobj_sp)
990 return return_valobj_sp;
991 }
992 }
993 return ValueObjectSP();
994}
995
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996bool
997Thread::IsThreadPlanDone (ThreadPlan *plan)
998{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999 if (!m_completed_plan_stack.empty())
1000 {
1001 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1002 {
1003 if (m_completed_plan_stack[i].get() == plan)
1004 return true;
1005 }
1006 }
1007 return false;
1008}
1009
1010bool
1011Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1012{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 if (!m_discarded_plan_stack.empty())
1014 {
1015 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1016 {
1017 if (m_discarded_plan_stack[i].get() == plan)
1018 return true;
1019 }
1020 }
1021 return false;
1022}
1023
1024ThreadPlan *
1025Thread::GetPreviousPlan (ThreadPlan *current_plan)
1026{
1027 if (current_plan == NULL)
1028 return NULL;
1029
1030 int stack_size = m_completed_plan_stack.size();
1031 for (int i = stack_size - 1; i > 0; i--)
1032 {
1033 if (current_plan == m_completed_plan_stack[i].get())
1034 return m_completed_plan_stack[i-1].get();
1035 }
1036
1037 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1038 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 if (m_plan_stack.size() > 0)
1040 return m_plan_stack.back().get();
1041 else
1042 return NULL;
1043 }
1044
1045 stack_size = m_plan_stack.size();
1046 for (int i = stack_size - 1; i > 0; i--)
1047 {
1048 if (current_plan == m_plan_stack[i].get())
1049 return m_plan_stack[i-1].get();
1050 }
1051 return NULL;
1052}
1053
1054void
1055Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1056{
1057 if (abort_other_plans)
1058 DiscardThreadPlans(true);
1059
1060 PushPlan (thread_plan_sp);
1061}
1062
Jim Ingham06e827c2010-11-11 19:26:09 +00001063
1064void
1065Thread::EnableTracer (bool value, bool single_stepping)
1066{
1067 int stack_size = m_plan_stack.size();
1068 for (int i = 0; i < stack_size; i++)
1069 {
1070 if (m_plan_stack[i]->GetThreadPlanTracer())
1071 {
1072 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1073 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1074 }
1075 }
1076}
1077
1078void
1079Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1080{
1081 int stack_size = m_plan_stack.size();
1082 for (int i = 0; i < stack_size; i++)
1083 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1084}
1085
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001087Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1088{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001089 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1090}
1091
1092void
1093Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1094{
Greg Clayton5160ce52013-03-27 23:08:40 +00001095 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001096 if (log)
1097 {
Daniel Malead01b2952012-11-29 21:49:15 +00001098 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 +00001099 }
1100
1101 int stack_size = m_plan_stack.size();
1102
1103 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1104 // stack, and if so discard up to and including it.
1105
Jim Ingham64e7ead2012-05-03 21:19:36 +00001106 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001107 {
1108 for (int i = stack_size - 1; i > 0; i--)
1109 DiscardPlan();
1110 }
1111 else
1112 {
1113 bool found_it = false;
1114 for (int i = stack_size - 1; i > 0; i--)
1115 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001116 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001117 found_it = true;
1118 }
1119 if (found_it)
1120 {
1121 bool last_one = false;
1122 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1123 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001124 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001125 last_one = true;
1126 DiscardPlan();
1127 }
1128 }
1129 }
1130 return;
1131}
1132
1133void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134Thread::DiscardThreadPlans(bool force)
1135{
Greg Clayton5160ce52013-03-27 23:08:40 +00001136 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137 if (log)
1138 {
Daniel Malead01b2952012-11-29 21:49:15 +00001139 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140 }
1141
1142 if (force)
1143 {
1144 int stack_size = m_plan_stack.size();
1145 for (int i = stack_size - 1; i > 0; i--)
1146 {
1147 DiscardPlan();
1148 }
1149 return;
1150 }
1151
1152 while (1)
1153 {
1154
1155 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001156 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157
1158 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1159 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1160 {
1161 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1162 {
1163 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1164 break;
1165 }
1166 }
1167
1168 if (discard)
1169 {
1170 // First pop all the dependent plans:
1171 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1172 {
1173
1174 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1175 // for the plan leaves it in a state that it is safe to pop the plan
1176 // with no more notice?
1177 DiscardPlan();
1178 }
1179
1180 // Now discard the master plan itself.
1181 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1182 // discard it's dependent plans, but not it...
1183 if (master_plan_idx > 0)
1184 {
1185 DiscardPlan();
1186 }
1187 }
1188 else
1189 {
1190 // If the master plan doesn't want to get discarded, then we're done.
1191 break;
1192 }
1193
1194 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195}
1196
Jim Inghamcf274f92012-04-09 22:37:39 +00001197bool
1198Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1199{
1200 if (plan_ptr->IsBasePlan())
1201 return true;
1202 else if (m_plan_stack.size() == 0)
1203 return false;
1204 else
1205 return m_plan_stack[0].get() == plan_ptr;
1206}
1207
Jim Ingham93208b82013-01-31 21:46:01 +00001208Error
1209Thread::UnwindInnermostExpression()
1210{
1211 Error error;
1212 int stack_size = m_plan_stack.size();
1213
1214 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1215 // stack, and if so discard up to and including it.
1216
1217 for (int i = stack_size - 1; i > 0; i--)
1218 {
1219 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1220 {
1221 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1222 return error;
1223 }
1224 }
1225 error.SetErrorString("No expressions currently active on this thread");
1226 return error;
1227}
1228
1229
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230ThreadPlan *
1231Thread::QueueFundamentalPlan (bool abort_other_plans)
1232{
1233 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1234 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1235 return thread_plan_sp.get();
1236}
1237
1238ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001239Thread::QueueThreadPlanForStepSingleInstruction
1240(
1241 bool step_over,
1242 bool abort_other_plans,
1243 bool stop_other_threads
1244)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245{
1246 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1247 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1248 return thread_plan_sp.get();
1249}
1250
1251ThreadPlan *
Jim Inghamc6276822012-12-12 19:58:40 +00001252Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001253(
1254 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001255 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001256 const SymbolContext &addr_context,
1257 lldb::RunMode stop_other_threads
1258)
1259{
1260 ThreadPlanSP thread_plan_sp;
1261 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1262
1263 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1264 return thread_plan_sp.get();
1265}
1266
1267ThreadPlan *
1268Thread::QueueThreadPlanForStepInRange
1269(
1270 bool abort_other_plans,
1271 const AddressRange &range,
1272 const SymbolContext &addr_context,
1273 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001274 lldb::RunMode stop_other_threads,
1275 bool avoid_code_without_debug_info
1276)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277{
1278 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001279 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1280 if (avoid_code_without_debug_info)
1281 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 else
Jim Inghamc6276822012-12-12 19:58:40 +00001283 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1284 if (step_in_target)
1285 plan->SetStepInTarget(step_in_target);
1286 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287
1288 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1289 return thread_plan_sp.get();
1290}
1291
1292
1293ThreadPlan *
1294Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1295{
1296 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1297 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1298 return thread_plan_sp.get();
1299}
1300
1301ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001302Thread::QueueThreadPlanForStepOut
1303(
1304 bool abort_other_plans,
1305 SymbolContext *addr_context,
1306 bool first_insn,
1307 bool stop_other_threads,
1308 Vote stop_vote,
1309 Vote run_vote,
1310 uint32_t frame_idx
1311)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312{
Greg Clayton481cef22011-01-21 06:11:58 +00001313 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1314 addr_context,
1315 first_insn,
1316 stop_other_threads,
1317 stop_vote,
1318 run_vote,
1319 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001320
1321 if (thread_plan_sp->ValidatePlan(NULL))
1322 {
1323 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1324 return thread_plan_sp.get();
1325 }
1326 else
1327 {
1328 return NULL;
1329 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330}
1331
1332ThreadPlan *
Jim Ingham18de2fd2012-05-10 01:35:39 +00001333Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001335 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001336 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1337 return NULL;
1338
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1340 return thread_plan_sp.get();
1341}
1342
1343ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1345 Address& function,
1346 lldb::addr_t arg,
1347 bool stop_other_threads,
Jim Ingham184e9812013-01-15 02:47:48 +00001348 bool unwind_on_error,
1349 bool ignore_breakpoints)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350{
Jim Ingham184e9812013-01-15 02:47:48 +00001351 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1352 function,
1353 ClangASTType(),
1354 arg,
1355 stop_other_threads,
1356 unwind_on_error,
1357 ignore_breakpoints));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1359 return thread_plan_sp.get();
1360}
1361
1362ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1364 Address &target_addr,
1365 bool stop_other_threads)
1366{
1367 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1368 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1369 return thread_plan_sp.get();
1370}
1371
1372ThreadPlan *
1373Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001374 lldb::addr_t *address_list,
1375 size_t num_addresses,
1376 bool stop_other_threads,
1377 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378{
Greg Clayton481cef22011-01-21 06:11:58 +00001379 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1381 return thread_plan_sp.get();
1382
1383}
1384
1385uint32_t
1386Thread::GetIndexID () const
1387{
1388 return m_index_id;
1389}
1390
1391void
1392Thread::DumpThreadPlans (lldb_private::Stream *s) const
1393{
1394 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001395 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001396 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001397 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 +00001398 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001401 s->Indent();
1402 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001405 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406 }
1407
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001409 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001411 s->Indent();
1412 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1413 for (i = stack_size - 1; i >= 0; i--)
1414 {
1415 s->IndentMore();
1416 s->Indent();
1417 s->Printf ("Element %d: ", i);
1418 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1419 s->EOL();
1420 s->IndentLess();
1421 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422 }
1423
1424 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001425 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001427 s->Indent();
1428 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1429 for (i = stack_size - 1; i >= 0; i--)
1430 {
1431 s->IndentMore();
1432 s->Indent();
1433 s->Printf ("Element %d: ", i);
1434 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1435 s->EOL();
1436 s->IndentLess();
1437 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438 }
1439
1440}
1441
Greg Claytond9e416c2012-02-18 05:35:26 +00001442TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001443Thread::CalculateTarget ()
1444{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001445 TargetSP target_sp;
1446 ProcessSP process_sp(GetProcess());
1447 if (process_sp)
1448 target_sp = process_sp->CalculateTarget();
1449 return target_sp;
1450
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451}
1452
Greg Claytond9e416c2012-02-18 05:35:26 +00001453ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454Thread::CalculateProcess ()
1455{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001456 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457}
1458
Greg Claytond9e416c2012-02-18 05:35:26 +00001459ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460Thread::CalculateThread ()
1461{
Greg Claytond9e416c2012-02-18 05:35:26 +00001462 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463}
1464
Greg Claytond9e416c2012-02-18 05:35:26 +00001465StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466Thread::CalculateStackFrame ()
1467{
Greg Claytond9e416c2012-02-18 05:35:26 +00001468 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469}
1470
1471void
Greg Clayton0603aa92010-10-04 01:05:56 +00001472Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001473{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001474 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475}
1476
Greg Clayton12daf9462010-08-25 00:35:26 +00001477
Greg Clayton39d0ab32012-03-29 01:41:38 +00001478StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001479Thread::GetStackFrameList ()
1480{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001481 StackFrameListSP frame_list_sp;
1482 Mutex::Locker locker(m_frame_mutex);
1483 if (m_curr_frames_sp)
1484 {
1485 frame_list_sp = m_curr_frames_sp;
1486 }
1487 else
1488 {
1489 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1490 m_curr_frames_sp = frame_list_sp;
1491 }
1492 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001493}
1494
Greg Clayton12daf9462010-08-25 00:35:26 +00001495void
1496Thread::ClearStackFrames ()
1497{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001498 Mutex::Locker locker(m_frame_mutex);
1499
Jim Inghamb0c72a52012-02-29 03:40:22 +00001500 // Only store away the old "reference" StackFrameList if we got all its frames:
1501 // FIXME: At some point we can try to splice in the frames we have fetched into
1502 // the new frame as we make it, but let's not try that now.
1503 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001504 m_prev_frames_sp.swap (m_curr_frames_sp);
1505 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001506}
1507
1508lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001509Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1510{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001511 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001512}
1513
Jim Ingham44137582012-09-12 00:40:39 +00001514
1515Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001516Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001517{
1518 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1519 Error return_error;
1520
1521 if (!frame_sp)
1522 {
Daniel Malead01b2952012-11-29 21:49:15 +00001523 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001524 }
1525
Jim Ingham4f465cf2012-10-10 18:32:14 +00001526 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001527}
1528
1529Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001530Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001531{
1532 Error return_error;
1533
1534 if (!frame_sp)
1535 {
1536 return_error.SetErrorString("Can't return to a null frame.");
1537 return return_error;
1538 }
1539
1540 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001541 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1542 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001543 if (!older_frame_sp)
1544 {
1545 return_error.SetErrorString("No older frame to return to.");
1546 return return_error;
1547 }
Jim Ingham44137582012-09-12 00:40:39 +00001548
1549 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001550 {
Jim Ingham44137582012-09-12 00:40:39 +00001551 lldb::ABISP abi = thread->GetProcess()->GetABI();
1552 if (!abi)
1553 {
1554 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001555 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001556 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001557 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1558
1559 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1560 // Turn that back on when that works.
1561 if (0 && sc.function != NULL)
1562 {
1563 Type *function_type = sc.function->GetType();
1564 if (function_type)
1565 {
1566 clang_type_t return_type = sc.function->GetReturnClangType();
1567 if (return_type)
1568 {
1569 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1570 StreamString s;
1571 ast_type.DumpTypeDescription(&s);
1572 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1573 if (cast_value_sp)
1574 {
1575 cast_value_sp->SetFormat(eFormatHex);
1576 return_value_sp = cast_value_sp;
1577 }
1578 }
1579 }
1580 }
1581
Jim Inghamcb640dd2012-09-14 02:14:15 +00001582 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001583 if (!return_error.Success())
1584 return return_error;
1585 }
1586
1587 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001588 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001589 // cook their data
1590
1591 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1592 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001593 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001594 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1595 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001596 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001597 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1598 if (copy_success)
1599 {
1600 thread->DiscardThreadPlans(true);
1601 thread->ClearStackFrames();
1602 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1603 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1604 }
1605 else
1606 {
1607 return_error.SetErrorString("Could not reset register values.");
1608 }
Jim Ingham93208b82013-01-31 21:46:01 +00001609 }
1610 else
1611 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001612 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001613 }
Jim Ingham44137582012-09-12 00:40:39 +00001614 }
1615 else
1616 {
Jim Ingham93208b82013-01-31 21:46:01 +00001617 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001618 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001619 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001620}
1621
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001622void
Greg Clayton0603aa92010-10-04 01:05:56 +00001623Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001624{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001625 ExecutionContext exe_ctx (shared_from_this());
1626 Process *process = exe_ctx.GetProcessPtr();
1627 if (process == NULL)
1628 return;
1629
Greg Claytonc14ee322011-09-22 04:58:26 +00001630 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001631 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001632 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001634 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 if (frame_sp)
1636 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001637 exe_ctx.SetFrameSP(frame_sp);
1638 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 }
1640 }
1641
Greg Clayton1ac04c32012-02-21 00:09:25 +00001642 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001643 assert (thread_format);
1644 const char *end = NULL;
1645 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001646 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001647 &exe_ctx,
1648 NULL,
1649 strm,
1650 &end);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651}
1652
Greg Clayton99d0faf2010-11-18 23:32:35 +00001653void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001654Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001655{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001656}
Jim Inghamee8aea12010-09-08 03:14:33 +00001657
Greg Clayton99d0faf2010-11-18 23:32:35 +00001658void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001659Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001660{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001661}
Jim Inghamee8aea12010-09-08 03:14:33 +00001662
Jim Inghame4284b72010-09-23 17:40:12 +00001663lldb::StackFrameSP
1664Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1665{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001666 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001667}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001668
1669const char *
1670Thread::StopReasonAsCString (lldb::StopReason reason)
1671{
1672 switch (reason)
1673 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001674 case eStopReasonInvalid: return "invalid";
1675 case eStopReasonNone: return "none";
1676 case eStopReasonTrace: return "trace";
1677 case eStopReasonBreakpoint: return "breakpoint";
1678 case eStopReasonWatchpoint: return "watchpoint";
1679 case eStopReasonSignal: return "signal";
1680 case eStopReasonException: return "exception";
1681 case eStopReasonExec: return "exec";
1682 case eStopReasonPlanComplete: return "plan complete";
1683 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001684 }
1685
1686
1687 static char unknown_state_string[64];
1688 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1689 return unknown_state_string;
1690}
1691
1692const char *
1693Thread::RunModeAsCString (lldb::RunMode mode)
1694{
1695 switch (mode)
1696 {
1697 case eOnlyThisThread: return "only this thread";
1698 case eAllThreads: return "all threads";
1699 case eOnlyDuringStepping: return "only during stepping";
1700 }
1701
1702 static char unknown_state_string[64];
1703 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1704 return unknown_state_string;
1705}
Jim Ingham06e827c2010-11-11 19:26:09 +00001706
Greg Clayton7260f622011-04-18 08:33:37 +00001707size_t
1708Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1709{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001710 ExecutionContext exe_ctx (shared_from_this());
1711 Target *target = exe_ctx.GetTargetPtr();
1712 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001713 size_t num_frames_shown = 0;
1714 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001715 bool is_selected = false;
1716 if (process)
1717 {
1718 if (process->GetThreadList().GetSelectedThread().get() == this)
1719 is_selected = true;
1720 }
1721 strm.Printf("%c ", is_selected ? '*' : ' ');
1722 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001723 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001724 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001725 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001726 {
Jim Inghame610d642011-08-16 00:07:28 +00001727 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1728 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1729 {
1730 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1731 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001732 }
Greg Clayton7260f622011-04-18 08:33:37 +00001733 }
1734
1735 DumpUsingSettingsFormat (strm, start_frame);
1736
1737 if (num_frames > 0)
1738 {
1739 strm.IndentMore();
1740
1741 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001742 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001743 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1744 start_frame,
1745 num_frames,
1746 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001747 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001748 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001749 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001750 }
1751 return num_frames_shown;
1752}
1753
1754size_t
1755Thread::GetStackFrameStatus (Stream& strm,
1756 uint32_t first_frame,
1757 uint32_t num_frames,
1758 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001759 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001760{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001761 return GetStackFrameList()->GetStatus (strm,
1762 first_frame,
1763 num_frames,
1764 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001765 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001766}
1767
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001768bool
1769Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1770{
1771 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1772 if (frame_sp)
1773 {
1774 checkpoint.SetStackID(frame_sp->GetStackID());
Greg Clayton1afa68e2013-04-02 20:32:37 +00001775 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1776 if (reg_ctx_sp)
1777 return reg_ctx_sp->ReadAllRegisterValues (checkpoint.GetData());
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001778 }
1779 return false;
1780}
Greg Clayton7260f622011-04-18 08:33:37 +00001781
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001782bool
1783Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1784{
Jim Ingham44137582012-09-12 00:40:39 +00001785 return ResetFrameZeroRegisters (checkpoint.GetData());
1786}
1787
1788bool
1789Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1790{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001791 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1792 if (frame_sp)
1793 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001794 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1795 if (reg_ctx_sp)
1796 {
1797 bool ret = reg_ctx_sp->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001798
Greg Clayton1afa68e2013-04-02 20:32:37 +00001799 // Clear out all stack frames as our world just changed.
1800 ClearStackFrames();
1801 reg_ctx_sp->InvalidateIfNeeded(true);
1802 if (m_unwinder_ap.get())
1803 m_unwinder_ap->Clear();
1804 return ret;
1805 }
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001806 }
1807 return false;
1808}
Greg Clayton7260f622011-04-18 08:33:37 +00001809
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001810Unwind *
1811Thread::GetUnwinder ()
1812{
1813 if (m_unwinder_ap.get() == NULL)
1814 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001815 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001816 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1817 switch (machine)
1818 {
1819 case llvm::Triple::x86_64:
1820 case llvm::Triple::x86:
1821 case llvm::Triple::arm:
1822 case llvm::Triple::thumb:
1823 m_unwinder_ap.reset (new UnwindLLDB (*this));
1824 break;
1825
1826 default:
1827 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1828 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1829 break;
1830 }
1831 }
1832 return m_unwinder_ap.get();
1833}
1834
1835
Greg Claytonfa559e52012-05-18 02:38:05 +00001836void
1837Thread::Flush ()
1838{
1839 ClearStackFrames ();
1840 m_reg_context_sp.reset();
1841}
Jim Ingham5d88a062012-10-16 00:09:33 +00001842
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00001843bool
Jim Ingham5d88a062012-10-16 00:09:33 +00001844Thread::IsStillAtLastBreakpointHit ()
1845{
1846 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1847 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1848 // multithreaded programs.
1849 if (m_actual_stop_info_sp) {
1850 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason();
1851 if (stop_reason == lldb::eStopReasonBreakpoint) {
1852 uint64_t value = m_actual_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00001853 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
1854 if (reg_ctx_sp)
1855 {
1856 lldb::addr_t pc = reg_ctx_sp->GetPC();
1857 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1858 if (bp_site_sp && value == bp_site_sp->GetID())
1859 return true;
1860 }
Jim Ingham5d88a062012-10-16 00:09:33 +00001861 }
1862 }
1863 return false;
1864}