blob: 5096a91cf1f6efe4eebfe1fa7deb5d4eba22914d [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"
Greg Clayton160c9d82013-05-01 21:54:04 +000016#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000019#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000020#include "lldb/Host/Host.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000021#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Target/DynamicLoader.h"
23#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000024#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000027#include "lldb/Target/StopInfo.h"
Greg Clayton896dff62010-07-23 16:45:51 +000028#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlan.h"
31#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/ThreadPlanBase.h"
33#include "lldb/Target/ThreadPlanStepInstruction.h"
34#include "lldb/Target/ThreadPlanStepOut.h"
35#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
36#include "lldb/Target/ThreadPlanStepThrough.h"
37#include "lldb/Target/ThreadPlanStepInRange.h"
38#include "lldb/Target/ThreadPlanStepOverRange.h"
39#include "lldb/Target/ThreadPlanRunToAddress.h"
40#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000041#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000042#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000043#include "Plugins/Process/Utility/UnwindLLDB.h"
44#include "UnwindMacOSXFrameBackchain.h"
45
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb;
48using namespace lldb_private;
49
Greg Clayton67cc0632012-08-22 17:17:09 +000050
51const ThreadPropertiesSP &
52Thread::GetGlobalProperties()
53{
54 static ThreadPropertiesSP g_settings_sp;
55 if (!g_settings_sp)
56 g_settings_sp.reset (new ThreadProperties (true));
57 return g_settings_sp;
58}
59
60static PropertyDefinition
61g_properties[] =
62{
63 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
64 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
65 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
66};
67
68enum {
69 ePropertyStepAvoidRegex,
70 ePropertyEnableThreadTrace
71};
72
73
74class ThreadOptionValueProperties : public OptionValueProperties
75{
76public:
77 ThreadOptionValueProperties (const ConstString &name) :
78 OptionValueProperties (name)
79 {
80 }
81
82 // This constructor is used when creating ThreadOptionValueProperties when it
83 // is part of a new lldb_private::Thread instance. It will copy all current
84 // global property values as needed
85 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000086 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000087 {
88 }
89
90 virtual const Property *
91 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
92 {
93 // When gettings the value for a key from the thread options, we will always
94 // try and grab the setting from the current thread if there is one. Else we just
95 // use the one from this instance.
96 if (exe_ctx)
97 {
98 Thread *thread = exe_ctx->GetThreadPtr();
99 if (thread)
100 {
101 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
102 if (this != instance_properties)
103 return instance_properties->ProtectedGetPropertyAtIndex (idx);
104 }
105 }
106 return ProtectedGetPropertyAtIndex (idx);
107 }
108};
109
110
111
112ThreadProperties::ThreadProperties (bool is_global) :
113 Properties ()
114{
115 if (is_global)
116 {
117 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
118 m_collection_sp->Initialize(g_properties);
119 }
120 else
121 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
122}
123
124ThreadProperties::~ThreadProperties()
125{
126}
127
128const RegularExpression *
129ThreadProperties::GetSymbolsToAvoidRegexp()
130{
131 const uint32_t idx = ePropertyStepAvoidRegex;
132 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
133}
134
135bool
136ThreadProperties::GetTraceEnabledState() const
137{
138 const uint32_t idx = ePropertyEnableThreadTrace;
139 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
140}
141
Jim Ingham4f465cf2012-10-10 18:32:14 +0000142//------------------------------------------------------------------
143// Thread Event Data
144//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000145
Jim Ingham4f465cf2012-10-10 18:32:14 +0000146
147const ConstString &
148Thread::ThreadEventData::GetFlavorString ()
149{
150 static ConstString g_flavor ("Thread::ThreadEventData");
151 return g_flavor;
152}
153
154Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
155 m_thread_sp (thread_sp),
156 m_stack_id ()
157{
158}
159
160Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
161 m_thread_sp (thread_sp),
162 m_stack_id (stack_id)
163{
164}
165
166Thread::ThreadEventData::ThreadEventData () :
167 m_thread_sp (),
168 m_stack_id ()
169{
170}
171
172Thread::ThreadEventData::~ThreadEventData ()
173{
174}
175
176void
177Thread::ThreadEventData::Dump (Stream *s) const
178{
179
180}
181
182const Thread::ThreadEventData *
183Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
184{
185 if (event_ptr)
186 {
187 const EventData *event_data = event_ptr->GetData();
188 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
189 return static_cast <const ThreadEventData *> (event_ptr->GetData());
190 }
191 return NULL;
192}
193
194ThreadSP
195Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
196{
197 ThreadSP thread_sp;
198 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
199 if (event_data)
200 thread_sp = event_data->GetThread();
201 return thread_sp;
202}
203
204StackID
205Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
206{
207 StackID stack_id;
208 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
209 if (event_data)
210 stack_id = event_data->GetStackID();
211 return stack_id;
212}
213
214StackFrameSP
215Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
216{
217 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
218 StackFrameSP frame_sp;
219 if (event_data)
220 {
221 ThreadSP thread_sp = event_data->GetThread();
222 if (thread_sp)
223 {
224 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
225 }
226 }
227 return frame_sp;
228}
229
230//------------------------------------------------------------------
231// Thread class
232//------------------------------------------------------------------
233
234ConstString &
235Thread::GetStaticBroadcasterClass ()
236{
237 static ConstString class_name ("lldb.thread");
238 return class_name;
239}
240
241Thread::Thread (Process &process, lldb::tid_t tid) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000242 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000244 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
245 m_process_wp (process.shared_from_this()),
Greg Claytonf4b47e12010-08-04 01:40:35 +0000246 m_actual_stop_info_sp (),
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000247 m_index_id (process.GetNextThreadIndexID(tid)),
Greg Clayton160c9d82013-05-01 21:54:04 +0000248 m_protocol_tid (tid),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000251 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000254 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000255 m_curr_frames_sp (),
256 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000258 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000259 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000260 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000261 m_destroy_called (false),
262 m_thread_stop_reason_stop_id (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263{
Greg Clayton5160ce52013-03-27 23:08:40 +0000264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000266 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
Jim Ingham4f465cf2012-10-10 18:32:14 +0000268 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 QueueFundamentalPlan(true);
270}
271
272
273Thread::~Thread()
274{
Greg Clayton5160ce52013-03-27 23:08:40 +0000275 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000277 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000278 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
279 assert (m_destroy_called);
280}
281
282void
283Thread::DestroyThread ()
284{
Greg Clayton35a4cc52012-10-29 20:52:08 +0000285 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000286 m_plan_stack.clear();
287 m_discarded_plan_stack.clear();
288 m_completed_plan_stack.clear();
Jim Inghamd8ba4642012-04-10 00:44:25 +0000289 m_actual_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000290 m_reg_context_sp.reset();
291 m_unwinder_ap.reset();
292 Mutex::Locker locker(m_frame_mutex);
293 m_curr_frames_sp.reset();
294 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295}
296
Jim Ingham4f465cf2012-10-10 18:32:14 +0000297void
298Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
299{
300 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
301 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
302}
303
304uint32_t
305Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
306{
307 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
308 if (broadcast)
309 BroadcastSelectedFrameChange(frame->GetStackID());
310 return ret_value;
311}
312
313bool
314Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
315{
316 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
317 if (frame_sp)
318 {
319 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
320 if (broadcast)
321 BroadcastSelectedFrameChange(frame_sp->GetStackID());
322 return true;
323 }
324 else
325 return false;
326}
327
Jim Ingham93208b82013-01-31 21:46:01 +0000328bool
329Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
330{
331 const bool broadcast = true;
332 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
333 if (success)
334 {
335 StackFrameSP frame_sp = GetSelectedFrame();
336 if (frame_sp)
337 {
338 bool already_shown = false;
339 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
340 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
341 {
342 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
343 }
344
345 bool show_frame_info = true;
346 bool show_source = !already_shown;
347 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
348 }
349 return false;
350 }
351 else
352 return false;
353}
354
Jim Ingham4f465cf2012-10-10 18:32:14 +0000355
Jim Inghamb15bfc72010-10-20 00:39:53 +0000356lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000357Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358{
Jim Inghamb15bfc72010-10-20 00:39:53 +0000359 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000360 ProcessSP process_sp (GetProcess());
361 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000362 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000363 {
Jim Ingham73ca05a2011-12-17 01:35:57 +0000364 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000365 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000366 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000367 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000368 if ((m_thread_stop_reason_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
369 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid())) // Stop info is valid, just return what we have
370 {
Jim Ingham79c35da2011-08-09 00:32:52 +0000371 return m_actual_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000372 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000373 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000374 {
375 GetPrivateStopReason ();
376 return m_actual_stop_info_sp;
377 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000378 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379}
380
Greg Clayton97d5cf02012-09-25 02:40:06 +0000381lldb::StopReason
382Thread::GetStopReason()
383{
384 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
385 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000386 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000387 return eStopReasonNone;
388}
389
390
391
Jim Ingham77787032011-01-20 02:03:18 +0000392void
393Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
394{
395 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham79c35da2011-08-09 00:32:52 +0000396 if (m_actual_stop_info_sp)
397 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000398 ProcessSP process_sp (GetProcess());
399 if (process_sp)
400 m_thread_stop_reason_stop_id = process_sp->GetStopID();
401 else
402 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham77787032011-01-20 02:03:18 +0000403}
404
405void
406Thread::SetStopInfoToNothing()
407{
408 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
409 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
410 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
411}
412
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413bool
414Thread::ThreadStoppedForAReason (void)
415{
Jim Inghamf94e1792012-08-11 00:35:26 +0000416 return (bool) GetPrivateStopReason ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417}
418
Jim Ingham77787032011-01-20 02:03:18 +0000419bool
420Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
421{
422 if (!SaveFrameZeroState(saved_state.register_backup))
423 return false;
424
425 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000426 ProcessSP process_sp (GetProcess());
427 if (process_sp)
428 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000429 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
430
Jim Ingham77787032011-01-20 02:03:18 +0000431 return true;
432}
433
434bool
Jim Ingham8559a352012-11-26 23:52:18 +0000435Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000436{
437 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham8559a352012-11-26 23:52:18 +0000438 return true;
439}
440
441bool
442Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
443{
Jim Ingham0c270682011-01-25 02:47:23 +0000444 if (saved_state.stop_info_sp)
445 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000446 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000447 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000448 return true;
449}
450
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451StateType
452Thread::GetState() const
453{
454 // If any other threads access this we will need a mutex for it
455 Mutex::Locker locker(m_state_mutex);
456 return m_state;
457}
458
459void
460Thread::SetState(StateType state)
461{
462 Mutex::Locker locker(m_state_mutex);
463 m_state = state;
464}
465
466void
467Thread::WillStop()
468{
469 ThreadPlan *current_plan = GetCurrentPlan();
470
471 // FIXME: I may decide to disallow threads with no plans. In which
472 // case this should go to an assert.
473
474 if (!current_plan)
475 return;
476
477 current_plan->WillStop();
478}
479
480void
481Thread::SetupForResume ()
482{
483 if (GetResumeState() != eStateSuspended)
484 {
485
486 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
487 // telling the current plan it will resume, since we might change what the current
488 // plan is.
489
Greg Clayton1afa68e2013-04-02 20:32:37 +0000490// StopReason stop_reason = lldb::eStopReasonInvalid;
491// StopInfoSP stop_info_sp = GetStopInfo();
492// if (stop_info_sp.get())
493// stop_reason = stop_info_sp->GetStopReason();
494// if (stop_reason == lldb::eStopReasonBreakpoint)
495 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
496 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000498 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
499 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000500 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000501 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
502 // special to step over a breakpoint.
503
504 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000505
Greg Clayton1afa68e2013-04-02 20:32:37 +0000506 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
507 {
508 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
509 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000511 ThreadPlanSP step_bp_plan_sp;
512 step_bp_plan->SetPrivate (true);
513
514 if (GetCurrentPlan()->RunState() != eStateStepping)
515 {
516 step_bp_plan->SetAutoContinue(true);
517 }
518 step_bp_plan_sp.reset (step_bp_plan);
519 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 }
522 }
523 }
524 }
525}
526
527bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000528Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529{
530 // At this point clear the completed plan stack.
531 m_completed_plan_stack.clear();
532 m_discarded_plan_stack.clear();
533
Greg Clayton97d5cf02012-09-25 02:40:06 +0000534 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000535
Greg Clayton160c9d82013-05-01 21:54:04 +0000536 // Make sure m_actual_stop_info_sp is valid
537 GetPrivateStopReason();
538
Jim Ingham92087d82012-01-31 23:09:20 +0000539 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
540 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
541 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
542 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000543 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham92087d82012-01-31 23:09:20 +0000544 if (m_thread_stop_reason_stop_id == process_stop_id &&
545 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
546 {
547 StopInfo *stop_info = GetPrivateStopReason().get();
548 if (stop_info)
549 stop_info->WillResume (resume_state);
550 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551
552 // Tell all the plans that we are about to resume in case they need to clear any state.
553 // We distinguish between the plan on the top of the stack and the lower
554 // plans in case a plan needs to do any special business before it runs.
555
Greg Claytond1d06e42013-04-20 00:27:58 +0000556 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000558 if (plan_ptr)
559 {
560 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561
Greg Claytond1d06e42013-04-20 00:27:58 +0000562 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
563 {
564 plan_ptr->WillResume (resume_state, false);
565 }
566
567 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
568 // In that case, don't reset it here.
569
570 if (need_to_resume && resume_state != eStateSuspended)
571 {
572 m_actual_stop_info_sp.reset();
573 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000574 }
575
Greg Clayton160c9d82013-05-01 21:54:04 +0000576 if (need_to_resume)
577 {
578 ClearStackFrames();
579 // Let Thread subclasses do any special work they need to prior to resuming
580 WillResume (resume_state);
581 }
582
Jim Ingham513c6bb2012-09-01 01:02:41 +0000583 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584}
585
586void
587Thread::DidResume ()
588{
589 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
590}
591
592bool
593Thread::ShouldStop (Event* event_ptr)
594{
595 ThreadPlan *current_plan = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000596
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 bool should_stop = true;
598
Greg Clayton5160ce52013-03-27 23:08:40 +0000599 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000600
601 if (GetResumeState () == eStateSuspended)
602 {
603 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000604 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000605 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000606 GetID (),
607 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000608 return false;
609 }
610
611 if (GetTemporaryResumeState () == eStateSuspended)
612 {
613 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000614 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000615 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000616 GetID (),
617 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000618 return false;
619 }
620
621 if (ThreadStoppedForAReason() == false)
622 {
623 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000624 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since no stop reason)",
Jim Ingham92087d82012-01-31 23:09:20 +0000625 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000626 GetID (),
627 GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000628 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000629 return false;
630 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000631
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 if (log)
633 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000634 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Jim Ingham92087d82012-01-31 23:09:20 +0000635 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000636 GetID (),
637 GetProtocolID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000638 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000639 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000641 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000643 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000645
646 // The top most plan always gets to do the trace log...
647 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000648
649 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
650 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
651 // do any more work on this stop.
652 StopInfoSP private_stop_info (GetPrivateStopReason());
653 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
654 {
655 if (log)
656 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
657 return false;
658 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659
Jim Inghambad39e42012-09-05 21:12:49 +0000660 // If we've already been restarted, don't query the plans since the state they would examine is not current.
661 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
662 return false;
663
664 // Before the plans see the state of the world, calculate the current inlined depth.
665 GetStackFrameList()->CalculateCurrentInlinedDepth();
666
Jim Ingham25f66702011-12-03 01:52:59 +0000667 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
668 // If that plan is still working, then we don't need to do any more work. If the plan that explains
669 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
670 // whether they still need to do more work.
671
672 bool done_processing_current_plan = false;
673
Jim Ingham0161b492013-02-09 01:29:05 +0000674 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000675 {
676 if (current_plan->TracerExplainsStop())
677 {
678 done_processing_current_plan = true;
679 should_stop = false;
680 }
681 else
682 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000683 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000684 // does and let it handle the situation.
685 ThreadPlan *plan_ptr = current_plan;
686 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
687 {
Jim Ingham0161b492013-02-09 01:29:05 +0000688 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000689 {
690 should_stop = plan_ptr->ShouldStop (event_ptr);
691
692 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
693 // and all the plans below it off the stack.
694
695 if (plan_ptr->MischiefManaged())
696 {
Jim Ingham031177e2012-05-11 23:49:49 +0000697 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000698 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000699
700 do
701 {
702 if (should_stop)
703 current_plan->WillStop();
704 PopPlan();
705 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000706 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
707 // Now, if the responsible plan was not "Okay to discard" then we're done,
708 // otherwise we forward this to the next plan in the stack below.
709 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
710 done_processing_current_plan = true;
711 else
712 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000713 }
714 else
715 done_processing_current_plan = true;
716
717 break;
718 }
719
720 }
721 }
722 }
723
724 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000726 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000727
Jim Ingham10c4b242011-10-15 00:23:43 +0000728 if (log)
729 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
730
Jim Ingham0f16e732011-02-08 05:20:59 +0000731 // We're starting from the base plan, so just let it decide;
732 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000734 should_stop = current_plan->ShouldStop (event_ptr);
735 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000736 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000737 }
738 else
739 {
740 // Otherwise, don't let the base plan override what the other plans say to do, since
741 // presumably if there were other plans they would know what to do...
742 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000744 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000746
747 should_stop = current_plan->ShouldStop(event_ptr);
748 if (log)
749 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
750 if (current_plan->MischiefManaged())
751 {
752 if (should_stop)
753 current_plan->WillStop();
754
755 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
756 // Otherwise, see if the plan's parent wants to stop.
757
758 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
759 {
760 PopPlan();
761 break;
762 }
763 else
764 {
765
766 PopPlan();
767
768 current_plan = GetCurrentPlan();
769 if (current_plan == NULL)
770 {
771 break;
772 }
773 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 }
775 else
776 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000777 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000778 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779 }
780 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000781
Jim Inghamb01e7422010-06-19 04:45:32 +0000782 if (over_ride_stop)
783 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000784
785 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
786 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
787 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
788 // This code clears stale plans off the stack.
789
790 if (should_stop)
791 {
792 ThreadPlan *plan_ptr = GetCurrentPlan();
793 while (!PlanIsBasePlan(plan_ptr))
794 {
795 bool stale = plan_ptr->IsPlanStale ();
796 ThreadPlan *examined_plan = plan_ptr;
797 plan_ptr = GetPreviousPlan (examined_plan);
798
799 if (stale)
800 {
801 if (log)
802 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
803 DiscardThreadPlansUpToPlan(examined_plan);
804 }
805 }
806 }
807
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809
Jim Ingham10c4b242011-10-15 00:23:43 +0000810 if (log)
811 {
812 StreamString s;
813 s.IndentMore();
814 DumpThreadPlans(&s);
815 log->Printf ("Plan stack final state:\n%s", s.GetData());
816 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
817 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 return should_stop;
819}
820
821Vote
822Thread::ShouldReportStop (Event* event_ptr)
823{
824 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000825 StateType temp_thread_state = GetTemporaryResumeState();
826
Greg Clayton5160ce52013-03-27 23:08:40 +0000827 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000828
829 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
830 {
831 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000832 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)", GetID(), eVoteNoOpinion);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000834 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835
Jim Ingham92087d82012-01-31 23:09:20 +0000836 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
837 {
838 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000839 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)", GetID(), eVoteNoOpinion);
Jim Ingham92087d82012-01-31 23:09:20 +0000840 return eVoteNoOpinion;
841 }
842
843 if (!ThreadStoppedForAReason())
844 {
845 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000846 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)", GetID(), eVoteNoOpinion);
Jim Ingham92087d82012-01-31 23:09:20 +0000847 return eVoteNoOpinion;
848 }
849
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850 if (m_completed_plan_stack.size() > 0)
851 {
852 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000853 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000854 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan", GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
856 }
857 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000858 {
Jim Ingham0161b492013-02-09 01:29:05 +0000859 Vote thread_vote = eVoteNoOpinion;
860 ThreadPlan *plan_ptr = GetCurrentPlan();
861 while (1)
862 {
863 if (plan_ptr->PlanExplainsStop(event_ptr))
864 {
865 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
866 break;
867 }
868 if (PlanIsBasePlan(plan_ptr))
869 break;
870 else
871 plan_ptr = GetPreviousPlan(plan_ptr);
872 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000873 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000874 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan", GetID(), thread_vote);
Jim Ingham0161b492013-02-09 01:29:05 +0000875
876 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000877 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878}
879
880Vote
881Thread::ShouldReportRun (Event* event_ptr)
882{
883 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000884
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885 if (thread_state == eStateSuspended
886 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000887 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000889 }
890
Greg Clayton5160ce52013-03-27 23:08:40 +0000891 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 if (m_completed_plan_stack.size() > 0)
893 {
894 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000895 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000896 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000897 GetIndexID(),
898 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000899 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000900 m_completed_plan_stack.back()->GetName());
901
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
903 }
904 else
Jim Ingham444586b2011-01-24 06:34:17 +0000905 {
906 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000907 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000908 GetIndexID(),
909 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000910 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000911 GetCurrentPlan()->GetName());
912
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000914 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915}
916
Jim Ingham1b54c882010-06-16 02:00:15 +0000917bool
918Thread::MatchesSpec (const ThreadSpec *spec)
919{
920 if (spec == NULL)
921 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000922
Jim Ingham3d902922012-03-07 22:03:04 +0000923 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +0000924}
925
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926void
927Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
928{
929 if (thread_plan_sp)
930 {
Jim Ingham06e827c2010-11-11 19:26:09 +0000931 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
932 if (!thread_plan_sp->GetThreadPlanTracer())
933 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +0000934 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +0000935
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 thread_plan_sp->DidPush();
937
Greg Clayton5160ce52013-03-27 23:08:40 +0000938 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 if (log)
940 {
941 StreamString s;
942 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Daniel Malead01b2952012-11-29 21:49:15 +0000943 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +0000945 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 }
947 }
948}
949
950void
951Thread::PopPlan ()
952{
Greg Clayton5160ce52013-03-27 23:08:40 +0000953 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000955 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956 return;
957 else
958 {
959 ThreadPlanSP &plan = m_plan_stack.back();
960 if (log)
961 {
Daniel Malead01b2952012-11-29 21:49:15 +0000962 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 }
964 m_completed_plan_stack.push_back (plan);
965 plan->WillPop();
966 m_plan_stack.pop_back();
967 }
968}
969
970void
971Thread::DiscardPlan ()
972{
973 if (m_plan_stack.size() > 1)
974 {
975 ThreadPlanSP &plan = m_plan_stack.back();
976 m_discarded_plan_stack.push_back (plan);
977 plan->WillPop();
978 m_plan_stack.pop_back();
979 }
980}
981
982ThreadPlan *
983Thread::GetCurrentPlan ()
984{
Jim Inghame1471232012-04-19 00:17:05 +0000985 // There will always be at least the base plan. If somebody is mucking with a
986 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +0000987 if (m_plan_stack.empty())
988 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +0000989 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990}
991
992ThreadPlanSP
993Thread::GetCompletedPlan ()
994{
995 ThreadPlanSP empty_plan_sp;
996 if (!m_completed_plan_stack.empty())
997 {
998 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
999 {
1000 ThreadPlanSP completed_plan_sp;
1001 completed_plan_sp = m_completed_plan_stack[i];
1002 if (!completed_plan_sp->GetPrivate ())
1003 return completed_plan_sp;
1004 }
1005 }
1006 return empty_plan_sp;
1007}
1008
Jim Ingham73ca05a2011-12-17 01:35:57 +00001009ValueObjectSP
1010Thread::GetReturnValueObject ()
1011{
1012 if (!m_completed_plan_stack.empty())
1013 {
1014 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1015 {
1016 ValueObjectSP return_valobj_sp;
1017 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1018 if (return_valobj_sp)
1019 return return_valobj_sp;
1020 }
1021 }
1022 return ValueObjectSP();
1023}
1024
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025bool
1026Thread::IsThreadPlanDone (ThreadPlan *plan)
1027{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028 if (!m_completed_plan_stack.empty())
1029 {
1030 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1031 {
1032 if (m_completed_plan_stack[i].get() == plan)
1033 return true;
1034 }
1035 }
1036 return false;
1037}
1038
1039bool
1040Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1041{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 if (!m_discarded_plan_stack.empty())
1043 {
1044 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1045 {
1046 if (m_discarded_plan_stack[i].get() == plan)
1047 return true;
1048 }
1049 }
1050 return false;
1051}
1052
1053ThreadPlan *
1054Thread::GetPreviousPlan (ThreadPlan *current_plan)
1055{
1056 if (current_plan == NULL)
1057 return NULL;
1058
1059 int stack_size = m_completed_plan_stack.size();
1060 for (int i = stack_size - 1; i > 0; i--)
1061 {
1062 if (current_plan == m_completed_plan_stack[i].get())
1063 return m_completed_plan_stack[i-1].get();
1064 }
1065
1066 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1067 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 if (m_plan_stack.size() > 0)
1069 return m_plan_stack.back().get();
1070 else
1071 return NULL;
1072 }
1073
1074 stack_size = m_plan_stack.size();
1075 for (int i = stack_size - 1; i > 0; i--)
1076 {
1077 if (current_plan == m_plan_stack[i].get())
1078 return m_plan_stack[i-1].get();
1079 }
1080 return NULL;
1081}
1082
1083void
1084Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1085{
1086 if (abort_other_plans)
1087 DiscardThreadPlans(true);
1088
1089 PushPlan (thread_plan_sp);
1090}
1091
Jim Ingham06e827c2010-11-11 19:26:09 +00001092
1093void
1094Thread::EnableTracer (bool value, bool single_stepping)
1095{
1096 int stack_size = m_plan_stack.size();
1097 for (int i = 0; i < stack_size; i++)
1098 {
1099 if (m_plan_stack[i]->GetThreadPlanTracer())
1100 {
1101 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1102 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1103 }
1104 }
1105}
1106
1107void
1108Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1109{
1110 int stack_size = m_plan_stack.size();
1111 for (int i = 0; i < stack_size; i++)
1112 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1113}
1114
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001116Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1117{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001118 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1119}
1120
1121void
1122Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1123{
Greg Clayton5160ce52013-03-27 23:08:40 +00001124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001125 if (log)
1126 {
Daniel Malead01b2952012-11-29 21:49:15 +00001127 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 +00001128 }
1129
1130 int stack_size = m_plan_stack.size();
1131
1132 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1133 // stack, and if so discard up to and including it.
1134
Jim Ingham64e7ead2012-05-03 21:19:36 +00001135 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001136 {
1137 for (int i = stack_size - 1; i > 0; i--)
1138 DiscardPlan();
1139 }
1140 else
1141 {
1142 bool found_it = false;
1143 for (int i = stack_size - 1; i > 0; i--)
1144 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001145 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001146 found_it = true;
1147 }
1148 if (found_it)
1149 {
1150 bool last_one = false;
1151 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1152 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001153 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001154 last_one = true;
1155 DiscardPlan();
1156 }
1157 }
1158 }
1159 return;
1160}
1161
1162void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163Thread::DiscardThreadPlans(bool force)
1164{
Greg Clayton5160ce52013-03-27 23:08:40 +00001165 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 if (log)
1167 {
Daniel Malead01b2952012-11-29 21:49:15 +00001168 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169 }
1170
1171 if (force)
1172 {
1173 int stack_size = m_plan_stack.size();
1174 for (int i = stack_size - 1; i > 0; i--)
1175 {
1176 DiscardPlan();
1177 }
1178 return;
1179 }
1180
1181 while (1)
1182 {
1183
1184 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001185 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186
1187 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1188 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1189 {
1190 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1191 {
1192 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1193 break;
1194 }
1195 }
1196
1197 if (discard)
1198 {
1199 // First pop all the dependent plans:
1200 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1201 {
1202
1203 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1204 // for the plan leaves it in a state that it is safe to pop the plan
1205 // with no more notice?
1206 DiscardPlan();
1207 }
1208
1209 // Now discard the master plan itself.
1210 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1211 // discard it's dependent plans, but not it...
1212 if (master_plan_idx > 0)
1213 {
1214 DiscardPlan();
1215 }
1216 }
1217 else
1218 {
1219 // If the master plan doesn't want to get discarded, then we're done.
1220 break;
1221 }
1222
1223 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224}
1225
Jim Inghamcf274f92012-04-09 22:37:39 +00001226bool
1227Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1228{
1229 if (plan_ptr->IsBasePlan())
1230 return true;
1231 else if (m_plan_stack.size() == 0)
1232 return false;
1233 else
1234 return m_plan_stack[0].get() == plan_ptr;
1235}
1236
Jim Ingham93208b82013-01-31 21:46:01 +00001237Error
1238Thread::UnwindInnermostExpression()
1239{
1240 Error error;
1241 int stack_size = m_plan_stack.size();
1242
1243 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1244 // stack, and if so discard up to and including it.
1245
1246 for (int i = stack_size - 1; i > 0; i--)
1247 {
1248 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1249 {
1250 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1251 return error;
1252 }
1253 }
1254 error.SetErrorString("No expressions currently active on this thread");
1255 return error;
1256}
1257
1258
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259ThreadPlan *
1260Thread::QueueFundamentalPlan (bool abort_other_plans)
1261{
1262 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1263 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1264 return thread_plan_sp.get();
1265}
1266
1267ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001268Thread::QueueThreadPlanForStepSingleInstruction
1269(
1270 bool step_over,
1271 bool abort_other_plans,
1272 bool stop_other_threads
1273)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274{
1275 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1276 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1277 return thread_plan_sp.get();
1278}
1279
1280ThreadPlan *
Jim Inghamc6276822012-12-12 19:58:40 +00001281Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001282(
1283 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001284 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001285 const SymbolContext &addr_context,
1286 lldb::RunMode stop_other_threads
1287)
1288{
1289 ThreadPlanSP thread_plan_sp;
1290 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1291
1292 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1293 return thread_plan_sp.get();
1294}
1295
1296ThreadPlan *
1297Thread::QueueThreadPlanForStepInRange
1298(
1299 bool abort_other_plans,
1300 const AddressRange &range,
1301 const SymbolContext &addr_context,
1302 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001303 lldb::RunMode stop_other_threads,
1304 bool avoid_code_without_debug_info
1305)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306{
1307 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001308 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1309 if (avoid_code_without_debug_info)
1310 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311 else
Jim Inghamc6276822012-12-12 19:58:40 +00001312 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1313 if (step_in_target)
1314 plan->SetStepInTarget(step_in_target);
1315 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316
1317 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1318 return thread_plan_sp.get();
1319}
1320
1321
1322ThreadPlan *
1323Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1324{
1325 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1326 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1327 return thread_plan_sp.get();
1328}
1329
1330ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001331Thread::QueueThreadPlanForStepOut
1332(
1333 bool abort_other_plans,
1334 SymbolContext *addr_context,
1335 bool first_insn,
1336 bool stop_other_threads,
1337 Vote stop_vote,
1338 Vote run_vote,
1339 uint32_t frame_idx
1340)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341{
Greg Clayton481cef22011-01-21 06:11:58 +00001342 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1343 addr_context,
1344 first_insn,
1345 stop_other_threads,
1346 stop_vote,
1347 run_vote,
1348 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001349
1350 if (thread_plan_sp->ValidatePlan(NULL))
1351 {
1352 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1353 return thread_plan_sp.get();
1354 }
1355 else
1356 {
1357 return NULL;
1358 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359}
1360
1361ThreadPlan *
Jim Ingham18de2fd2012-05-10 01:35:39 +00001362Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001364 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001365 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1366 return NULL;
1367
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1369 return thread_plan_sp.get();
1370}
1371
1372ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1374 Address& function,
1375 lldb::addr_t arg,
1376 bool stop_other_threads,
Jim Ingham184e9812013-01-15 02:47:48 +00001377 bool unwind_on_error,
1378 bool ignore_breakpoints)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379{
Jim Ingham184e9812013-01-15 02:47:48 +00001380 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1381 function,
1382 ClangASTType(),
1383 arg,
1384 stop_other_threads,
1385 unwind_on_error,
1386 ignore_breakpoints));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1388 return thread_plan_sp.get();
1389}
1390
1391ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1393 Address &target_addr,
1394 bool stop_other_threads)
1395{
1396 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1397 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1398 return thread_plan_sp.get();
1399}
1400
1401ThreadPlan *
1402Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001403 lldb::addr_t *address_list,
1404 size_t num_addresses,
1405 bool stop_other_threads,
1406 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407{
Greg Clayton481cef22011-01-21 06:11:58 +00001408 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1410 return thread_plan_sp.get();
1411
1412}
1413
1414uint32_t
1415Thread::GetIndexID () const
1416{
1417 return m_index_id;
1418}
1419
1420void
1421Thread::DumpThreadPlans (lldb_private::Stream *s) const
1422{
1423 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001424 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001425 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001426 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 +00001427 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001430 s->Indent();
1431 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001434 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435 }
1436
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001438 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001440 s->Indent();
1441 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1442 for (i = stack_size - 1; i >= 0; i--)
1443 {
1444 s->IndentMore();
1445 s->Indent();
1446 s->Printf ("Element %d: ", i);
1447 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1448 s->EOL();
1449 s->IndentLess();
1450 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 }
1452
1453 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001454 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001456 s->Indent();
1457 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1458 for (i = stack_size - 1; i >= 0; i--)
1459 {
1460 s->IndentMore();
1461 s->Indent();
1462 s->Printf ("Element %d: ", i);
1463 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1464 s->EOL();
1465 s->IndentLess();
1466 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 }
1468
1469}
1470
Greg Claytond9e416c2012-02-18 05:35:26 +00001471TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472Thread::CalculateTarget ()
1473{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001474 TargetSP target_sp;
1475 ProcessSP process_sp(GetProcess());
1476 if (process_sp)
1477 target_sp = process_sp->CalculateTarget();
1478 return target_sp;
1479
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480}
1481
Greg Claytond9e416c2012-02-18 05:35:26 +00001482ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483Thread::CalculateProcess ()
1484{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001485 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486}
1487
Greg Claytond9e416c2012-02-18 05:35:26 +00001488ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489Thread::CalculateThread ()
1490{
Greg Claytond9e416c2012-02-18 05:35:26 +00001491 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492}
1493
Greg Claytond9e416c2012-02-18 05:35:26 +00001494StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495Thread::CalculateStackFrame ()
1496{
Greg Claytond9e416c2012-02-18 05:35:26 +00001497 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498}
1499
1500void
Greg Clayton0603aa92010-10-04 01:05:56 +00001501Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001503 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504}
1505
Greg Clayton12daf9462010-08-25 00:35:26 +00001506
Greg Clayton39d0ab32012-03-29 01:41:38 +00001507StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001508Thread::GetStackFrameList ()
1509{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001510 StackFrameListSP frame_list_sp;
1511 Mutex::Locker locker(m_frame_mutex);
1512 if (m_curr_frames_sp)
1513 {
1514 frame_list_sp = m_curr_frames_sp;
1515 }
1516 else
1517 {
1518 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1519 m_curr_frames_sp = frame_list_sp;
1520 }
1521 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001522}
1523
Greg Clayton12daf9462010-08-25 00:35:26 +00001524void
1525Thread::ClearStackFrames ()
1526{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001527 Mutex::Locker locker(m_frame_mutex);
1528
Greg Clayton160c9d82013-05-01 21:54:04 +00001529 Unwind *unwinder = GetUnwinder ();
1530 if (unwinder)
1531 unwinder->Clear();
1532
Jim Inghamb0c72a52012-02-29 03:40:22 +00001533 // Only store away the old "reference" StackFrameList if we got all its frames:
1534 // FIXME: At some point we can try to splice in the frames we have fetched into
1535 // the new frame as we make it, but let's not try that now.
1536 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001537 m_prev_frames_sp.swap (m_curr_frames_sp);
1538 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001539}
1540
1541lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001542Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1543{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001544 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001545}
1546
Jim Ingham44137582012-09-12 00:40:39 +00001547
1548Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001549Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001550{
1551 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1552 Error return_error;
1553
1554 if (!frame_sp)
1555 {
Daniel Malead01b2952012-11-29 21:49:15 +00001556 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001557 }
1558
Jim Ingham4f465cf2012-10-10 18:32:14 +00001559 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001560}
1561
1562Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001563Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001564{
1565 Error return_error;
1566
1567 if (!frame_sp)
1568 {
1569 return_error.SetErrorString("Can't return to a null frame.");
1570 return return_error;
1571 }
1572
1573 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001574 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1575 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001576 if (!older_frame_sp)
1577 {
1578 return_error.SetErrorString("No older frame to return to.");
1579 return return_error;
1580 }
Jim Ingham44137582012-09-12 00:40:39 +00001581
1582 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001583 {
Jim Ingham44137582012-09-12 00:40:39 +00001584 lldb::ABISP abi = thread->GetProcess()->GetABI();
1585 if (!abi)
1586 {
1587 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001588 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001589 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001590 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1591
1592 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1593 // Turn that back on when that works.
1594 if (0 && sc.function != NULL)
1595 {
1596 Type *function_type = sc.function->GetType();
1597 if (function_type)
1598 {
1599 clang_type_t return_type = sc.function->GetReturnClangType();
1600 if (return_type)
1601 {
1602 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1603 StreamString s;
1604 ast_type.DumpTypeDescription(&s);
1605 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1606 if (cast_value_sp)
1607 {
1608 cast_value_sp->SetFormat(eFormatHex);
1609 return_value_sp = cast_value_sp;
1610 }
1611 }
1612 }
1613 }
1614
Jim Inghamcb640dd2012-09-14 02:14:15 +00001615 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001616 if (!return_error.Success())
1617 return return_error;
1618 }
1619
1620 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001621 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001622 // cook their data
1623
1624 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1625 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001626 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001627 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1628 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001629 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001630 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1631 if (copy_success)
1632 {
1633 thread->DiscardThreadPlans(true);
1634 thread->ClearStackFrames();
1635 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1636 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1637 }
1638 else
1639 {
1640 return_error.SetErrorString("Could not reset register values.");
1641 }
Jim Ingham93208b82013-01-31 21:46:01 +00001642 }
1643 else
1644 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001645 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001646 }
Jim Ingham44137582012-09-12 00:40:39 +00001647 }
1648 else
1649 {
Jim Ingham93208b82013-01-31 21:46:01 +00001650 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001651 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001652 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001653}
1654
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655void
Greg Clayton0603aa92010-10-04 01:05:56 +00001656Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001658 ExecutionContext exe_ctx (shared_from_this());
1659 Process *process = exe_ctx.GetProcessPtr();
1660 if (process == NULL)
1661 return;
1662
Greg Claytonc14ee322011-09-22 04:58:26 +00001663 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001664 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001665 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001667 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668 if (frame_sp)
1669 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001670 exe_ctx.SetFrameSP(frame_sp);
1671 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001672 }
1673 }
1674
Greg Clayton1ac04c32012-02-21 00:09:25 +00001675 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001676 assert (thread_format);
1677 const char *end = NULL;
1678 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001679 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001680 &exe_ctx,
1681 NULL,
1682 strm,
1683 &end);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684}
1685
Greg Clayton99d0faf2010-11-18 23:32:35 +00001686void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001687Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001688{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001689}
Jim Inghamee8aea12010-09-08 03:14:33 +00001690
Greg Clayton99d0faf2010-11-18 23:32:35 +00001691void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001692Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001693{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001694}
Jim Inghamee8aea12010-09-08 03:14:33 +00001695
Jim Inghame4284b72010-09-23 17:40:12 +00001696lldb::StackFrameSP
1697Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1698{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001699 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001700}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001701
1702const char *
1703Thread::StopReasonAsCString (lldb::StopReason reason)
1704{
1705 switch (reason)
1706 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001707 case eStopReasonInvalid: return "invalid";
1708 case eStopReasonNone: return "none";
1709 case eStopReasonTrace: return "trace";
1710 case eStopReasonBreakpoint: return "breakpoint";
1711 case eStopReasonWatchpoint: return "watchpoint";
1712 case eStopReasonSignal: return "signal";
1713 case eStopReasonException: return "exception";
1714 case eStopReasonExec: return "exec";
1715 case eStopReasonPlanComplete: return "plan complete";
1716 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001717 }
1718
1719
1720 static char unknown_state_string[64];
1721 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1722 return unknown_state_string;
1723}
1724
1725const char *
1726Thread::RunModeAsCString (lldb::RunMode mode)
1727{
1728 switch (mode)
1729 {
1730 case eOnlyThisThread: return "only this thread";
1731 case eAllThreads: return "all threads";
1732 case eOnlyDuringStepping: return "only during stepping";
1733 }
1734
1735 static char unknown_state_string[64];
1736 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1737 return unknown_state_string;
1738}
Jim Ingham06e827c2010-11-11 19:26:09 +00001739
Greg Clayton7260f622011-04-18 08:33:37 +00001740size_t
1741Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1742{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001743 ExecutionContext exe_ctx (shared_from_this());
1744 Target *target = exe_ctx.GetTargetPtr();
1745 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001746 size_t num_frames_shown = 0;
1747 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001748 bool is_selected = false;
1749 if (process)
1750 {
1751 if (process->GetThreadList().GetSelectedThread().get() == this)
1752 is_selected = true;
1753 }
1754 strm.Printf("%c ", is_selected ? '*' : ' ');
1755 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001756 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001757 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001758 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001759 {
Jim Inghame610d642011-08-16 00:07:28 +00001760 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1761 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1762 {
1763 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1764 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001765 }
Greg Clayton7260f622011-04-18 08:33:37 +00001766 }
1767
1768 DumpUsingSettingsFormat (strm, start_frame);
1769
1770 if (num_frames > 0)
1771 {
1772 strm.IndentMore();
1773
1774 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001775 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001776 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1777 start_frame,
1778 num_frames,
1779 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001780 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001781 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001782 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001783 }
1784 return num_frames_shown;
1785}
1786
1787size_t
1788Thread::GetStackFrameStatus (Stream& strm,
1789 uint32_t first_frame,
1790 uint32_t num_frames,
1791 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001792 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001793{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001794 return GetStackFrameList()->GetStatus (strm,
1795 first_frame,
1796 num_frames,
1797 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001798 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001799}
1800
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001801bool
1802Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1803{
1804 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1805 if (frame_sp)
1806 {
1807 checkpoint.SetStackID(frame_sp->GetStackID());
Greg Clayton1afa68e2013-04-02 20:32:37 +00001808 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1809 if (reg_ctx_sp)
1810 return reg_ctx_sp->ReadAllRegisterValues (checkpoint.GetData());
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001811 }
1812 return false;
1813}
Greg Clayton7260f622011-04-18 08:33:37 +00001814
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001815bool
1816Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1817{
Jim Ingham44137582012-09-12 00:40:39 +00001818 return ResetFrameZeroRegisters (checkpoint.GetData());
1819}
1820
1821bool
1822Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1823{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001824 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1825 if (frame_sp)
1826 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001827 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1828 if (reg_ctx_sp)
1829 {
1830 bool ret = reg_ctx_sp->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001831
Greg Clayton1afa68e2013-04-02 20:32:37 +00001832 // Clear out all stack frames as our world just changed.
1833 ClearStackFrames();
1834 reg_ctx_sp->InvalidateIfNeeded(true);
1835 if (m_unwinder_ap.get())
1836 m_unwinder_ap->Clear();
1837 return ret;
1838 }
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001839 }
1840 return false;
1841}
Greg Clayton7260f622011-04-18 08:33:37 +00001842
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001843Unwind *
1844Thread::GetUnwinder ()
1845{
1846 if (m_unwinder_ap.get() == NULL)
1847 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001848 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001849 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1850 switch (machine)
1851 {
1852 case llvm::Triple::x86_64:
1853 case llvm::Triple::x86:
1854 case llvm::Triple::arm:
1855 case llvm::Triple::thumb:
1856 m_unwinder_ap.reset (new UnwindLLDB (*this));
1857 break;
1858
1859 default:
1860 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1861 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1862 break;
1863 }
1864 }
1865 return m_unwinder_ap.get();
1866}
1867
1868
Greg Claytonfa559e52012-05-18 02:38:05 +00001869void
1870Thread::Flush ()
1871{
1872 ClearStackFrames ();
1873 m_reg_context_sp.reset();
1874}
Jim Ingham5d88a062012-10-16 00:09:33 +00001875
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00001876bool
Jim Ingham5d88a062012-10-16 00:09:33 +00001877Thread::IsStillAtLastBreakpointHit ()
1878{
1879 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1880 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1881 // multithreaded programs.
1882 if (m_actual_stop_info_sp) {
1883 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason();
1884 if (stop_reason == lldb::eStopReasonBreakpoint) {
1885 uint64_t value = m_actual_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00001886 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
1887 if (reg_ctx_sp)
1888 {
1889 lldb::addr_t pc = reg_ctx_sp->GetPC();
1890 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1891 if (bp_site_sp && value == bp_site_sp->GetID())
1892 return true;
1893 }
Jim Ingham5d88a062012-10-16 00:09:33 +00001894 }
1895 }
1896 return false;
1897}