blob: b65434bf94826603729bba7d8eee96a319e55eb1 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/lldb-private-log.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000013#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Claytona830adb2010-10-04 01:05:56 +000014#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
Greg Claytona46013b2013-05-01 21:54:04 +000016#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamString.h"
Jim Ingham20594b12010-09-08 03:14:33 +000019#include "lldb/Core/RegularExpression.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000020#include "lldb/Host/Host.h"
Jim Ingham46657452012-09-27 01:15:29 +000021#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/DynamicLoader.h"
23#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000024#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000027#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000028#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlan.h"
31#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-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 Ingham3c7b5b92010-06-16 02:00:15 +000041#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000042#include "lldb/Target/Unwind.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000043#include "Plugins/Process/Utility/UnwindLLDB.h"
44#include "UnwindMacOSXFrameBackchain.h"
45
Chris Lattner24943d22010-06-08 16:52:24 +000046
47using namespace lldb;
48using namespace lldb_private;
49
Greg Clayton73844aa2012-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 Claytonc6e82e42012-08-22 18:39:03 +000086 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton73844aa2012-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 Ingham94a5d0d2012-10-10 18:32:14 +0000142//------------------------------------------------------------------
143// Thread Event Data
144//------------------------------------------------------------------
Greg Clayton73844aa2012-08-22 17:17:09 +0000145
Jim Ingham94a5d0d2012-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 Clayton73844aa2012-08-22 17:17:09 +0000242 ThreadProperties (false),
Chris Lattner24943d22010-06-08 16:52:24 +0000243 UserID (tid),
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000244 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
245 m_process_wp (process.shared_from_this()),
Greg Clayton863aa282013-05-09 01:55:29 +0000246 m_stop_info_sp (),
247 m_stop_info_stop_id (0),
Han Ming Ongccd5c4e2013-01-08 22:10:01 +0000248 m_index_id (process.GetNextThreadIndexID(tid)),
Chris Lattner24943d22010-06-08 16:52:24 +0000249 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000250 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000251 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000252 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000253 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000254 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000255 m_curr_frames_sp (),
256 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000257 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000258 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000259 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000260 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000261 m_destroy_called (false),
Jim Ingham7c79a272013-05-08 00:35:16 +0000262 m_override_should_notify (eLazyBoolCalculate)
Chris Lattner24943d22010-06-08 16:52:24 +0000263{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000265 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000266 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000267
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000268 CheckInWithManager();
Chris Lattner24943d22010-06-08 16:52:24 +0000269 QueueFundamentalPlan(true);
270}
271
272
273Thread::~Thread()
274{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000275 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000276 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000277 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Jim Inghamcdea2362010-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 Clayton863aa282013-05-09 01:55:29 +0000285 // Tell any plans on the plan stack that the thread is being destroyed since
286 // any active plans that have a thread go away in the middle of might need
287 // to do cleanup.
288 for (auto plan : m_plan_stack)
289 plan->ThreadDestroyed();
290
Greg Clayton84332782012-10-29 20:52:08 +0000291 m_destroy_called = true;
Jim Inghamcdea2362010-11-18 02:47:07 +0000292 m_plan_stack.clear();
293 m_discarded_plan_stack.clear();
294 m_completed_plan_stack.clear();
Greg Claytone4923dd2013-07-30 00:23:06 +0000295
296 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
297 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
298 // without checking first whether it is destroyed, they won't crash.
299 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
300 m_plan_stack.push_back (null_plan_sp);
301
Greg Clayton863aa282013-05-09 01:55:29 +0000302 m_stop_info_sp.reset();
Greg Clayton84332782012-10-29 20:52:08 +0000303 m_reg_context_sp.reset();
304 m_unwinder_ap.reset();
305 Mutex::Locker locker(m_frame_mutex);
306 m_curr_frames_sp.reset();
307 m_prev_frames_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000308}
309
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000310void
311Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
312{
313 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
314 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
315}
316
317uint32_t
318Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
319{
320 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
321 if (broadcast)
322 BroadcastSelectedFrameChange(frame->GetStackID());
323 return ret_value;
324}
325
326bool
327Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
328{
329 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
330 if (frame_sp)
331 {
332 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
333 if (broadcast)
334 BroadcastSelectedFrameChange(frame_sp->GetStackID());
335 return true;
336 }
337 else
338 return false;
339}
340
Jim Inghambe51f8a2013-01-31 21:46:01 +0000341bool
342Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
343{
344 const bool broadcast = true;
345 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
346 if (success)
347 {
348 StackFrameSP frame_sp = GetSelectedFrame();
349 if (frame_sp)
350 {
351 bool already_shown = false;
352 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
353 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
354 {
355 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
356 }
357
358 bool show_frame_info = true;
359 bool show_source = !already_shown;
360 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
361 }
362 return false;
363 }
364 else
365 return false;
366}
367
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000368
Jim Ingham6297a3a2010-10-20 00:39:53 +0000369lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000370Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000371{
Greg Claytone4923dd2013-07-30 00:23:06 +0000372 if (m_destroy_called)
373 return m_stop_info_sp;
374
Jim Ingham6297a3a2010-10-20 00:39:53 +0000375 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylor4e75e352013-05-07 18:35:34 +0000376 ProcessSP process_sp (GetProcess());
377 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Ingham707b7a82012-05-01 18:38:37 +0000378 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylor4e75e352013-05-07 18:35:34 +0000379 {
Jim Ingham1586d972011-12-17 01:35:57 +0000380 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Andrew Kaylor4e75e352013-05-07 18:35:34 +0000381 }
Jim Ingham6297a3a2010-10-20 00:39:53 +0000382 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000383 {
Greg Clayton863aa282013-05-09 01:55:29 +0000384 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
385 (m_stop_info_sp && m_stop_info_sp->IsValid())) // Stop info is valid, just return what we have
Andrew Kaylor4e75e352013-05-07 18:35:34 +0000386 {
Greg Clayton863aa282013-05-09 01:55:29 +0000387 return m_stop_info_sp;
Andrew Kaylor4e75e352013-05-07 18:35:34 +0000388 }
Jim Ingham24e0d612011-08-09 00:32:52 +0000389 else
Andrew Kaylor4e75e352013-05-07 18:35:34 +0000390 {
Greg Clayton863aa282013-05-09 01:55:29 +0000391 GetPrivateStopInfo ();
392 return m_stop_info_sp;
Andrew Kaylor4e75e352013-05-07 18:35:34 +0000393 }
Jim Ingham24e0d612011-08-09 00:32:52 +0000394 }
Chris Lattner24943d22010-06-08 16:52:24 +0000395}
396
Greg Clayton863aa282013-05-09 01:55:29 +0000397lldb::StopInfoSP
398Thread::GetPrivateStopInfo ()
399{
Greg Claytone4923dd2013-07-30 00:23:06 +0000400 if (m_destroy_called)
401 return m_stop_info_sp;
402
Greg Clayton863aa282013-05-09 01:55:29 +0000403 ProcessSP process_sp (GetProcess());
404 if (process_sp)
405 {
Daniel Malea375ba882013-05-14 15:20:12 +0000406 const uint32_t process_stop_id = process_sp->GetStopID();
407 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton863aa282013-05-09 01:55:29 +0000408 {
Daniel Malea375ba882013-05-14 15:20:12 +0000409 if (m_stop_info_sp)
Greg Clayton863aa282013-05-09 01:55:29 +0000410 {
Daniel Malea375ba882013-05-14 15:20:12 +0000411 if (m_stop_info_sp->IsValid()
412 || IsStillAtLastBreakpointHit()
413 || GetCurrentPlan()->IsVirtualStep())
414 SetStopInfo (m_stop_info_sp);
415 else
416 m_stop_info_sp.reset();
417 }
418
419 if (!m_stop_info_sp)
420 {
421 if (CalculateStopInfo() == false)
422 SetStopInfo (StopInfoSP());
Greg Clayton863aa282013-05-09 01:55:29 +0000423 }
424 }
425 }
426 return m_stop_info_sp;
427}
428
429
Greg Clayton3acaa922012-09-25 02:40:06 +0000430lldb::StopReason
431Thread::GetStopReason()
432{
433 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
434 if (stop_info_sp)
Filipe Cabecinhasc50a1dd2012-09-28 15:55:43 +0000435 return stop_info_sp->GetStopReason();
Greg Clayton3acaa922012-09-25 02:40:06 +0000436 return eStopReasonNone;
437}
438
439
440
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000441void
442Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
443{
Greg Clayton863aa282013-05-09 01:55:29 +0000444 m_stop_info_sp = stop_info_sp;
445 if (m_stop_info_sp)
Jim Ingham7c79a272013-05-08 00:35:16 +0000446 {
Greg Clayton863aa282013-05-09 01:55:29 +0000447 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham7c79a272013-05-08 00:35:16 +0000448 // If we are overriding the ShouldReportStop, do that here:
449 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton863aa282013-05-09 01:55:29 +0000450 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham7c79a272013-05-08 00:35:16 +0000451 }
452
Greg Claytonf4124de2012-02-21 00:09:25 +0000453 ProcessSP process_sp (GetProcess());
454 if (process_sp)
Greg Clayton863aa282013-05-09 01:55:29 +0000455 m_stop_info_stop_id = process_sp->GetStopID();
Greg Claytonf4124de2012-02-21 00:09:25 +0000456 else
Greg Clayton863aa282013-05-09 01:55:29 +0000457 m_stop_info_stop_id = UINT32_MAX;
Greg Claytone3f9bfd2013-05-21 21:55:59 +0000458 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
459 if (log)
Matt Kopec884288b2013-06-03 18:00:07 +0000460 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)\n", this, GetID(), stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>", m_stop_info_stop_id);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000461}
462
463void
Jim Ingham7c79a272013-05-08 00:35:16 +0000464Thread::SetShouldReportStop (Vote vote)
465{
466 if (vote == eVoteNoOpinion)
467 return;
468 else
469 {
470 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton863aa282013-05-09 01:55:29 +0000471 if (m_stop_info_sp)
472 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham7c79a272013-05-08 00:35:16 +0000473 }
474}
475
476void
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000477Thread::SetStopInfoToNothing()
478{
479 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
480 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
481 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
482}
483
Chris Lattner24943d22010-06-08 16:52:24 +0000484bool
485Thread::ThreadStoppedForAReason (void)
486{
Greg Clayton863aa282013-05-09 01:55:29 +0000487 return (bool) GetPrivateStopInfo ();
Chris Lattner24943d22010-06-08 16:52:24 +0000488}
489
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000490bool
491Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
492{
493 if (!SaveFrameZeroState(saved_state.register_backup))
494 return false;
495
496 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000497 ProcessSP process_sp (GetProcess());
498 if (process_sp)
499 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham36de3c02012-09-07 23:36:43 +0000500 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
501
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000502 return true;
503}
504
505bool
Jim Ingham76b258d2012-11-26 23:52:18 +0000506Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000507{
508 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham76b258d2012-11-26 23:52:18 +0000509 return true;
510}
511
512bool
513Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
514{
Jim Ingham11a837d2011-01-25 02:47:23 +0000515 if (saved_state.stop_info_sp)
516 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000517 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham36de3c02012-09-07 23:36:43 +0000518 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000519 return true;
520}
521
Chris Lattner24943d22010-06-08 16:52:24 +0000522StateType
523Thread::GetState() const
524{
525 // If any other threads access this we will need a mutex for it
526 Mutex::Locker locker(m_state_mutex);
527 return m_state;
528}
529
530void
531Thread::SetState(StateType state)
532{
533 Mutex::Locker locker(m_state_mutex);
534 m_state = state;
535}
536
537void
538Thread::WillStop()
539{
540 ThreadPlan *current_plan = GetCurrentPlan();
541
542 // FIXME: I may decide to disallow threads with no plans. In which
543 // case this should go to an assert.
544
545 if (!current_plan)
546 return;
547
548 current_plan->WillStop();
549}
550
551void
552Thread::SetupForResume ()
553{
554 if (GetResumeState() != eStateSuspended)
555 {
556
557 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
558 // telling the current plan it will resume, since we might change what the current
559 // plan is.
560
Greg Clayton4ee040c2013-04-02 20:32:37 +0000561// StopReason stop_reason = lldb::eStopReasonInvalid;
562// StopInfoSP stop_info_sp = GetStopInfo();
563// if (stop_info_sp.get())
564// stop_reason = stop_info_sp->GetStopReason();
565// if (stop_reason == lldb::eStopReasonBreakpoint)
566 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
567 if (reg_ctx_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000568 {
Greg Clayton4ee040c2013-04-02 20:32:37 +0000569 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
570 if (bp_site_sp)
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000571 {
Greg Clayton4ee040c2013-04-02 20:32:37 +0000572 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
573 // special to step over a breakpoint.
574
575 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000576
Greg Clayton4ee040c2013-04-02 20:32:37 +0000577 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
578 {
579 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
580 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000581 {
Greg Clayton4ee040c2013-04-02 20:32:37 +0000582 ThreadPlanSP step_bp_plan_sp;
583 step_bp_plan->SetPrivate (true);
584
585 if (GetCurrentPlan()->RunState() != eStateStepping)
586 {
587 step_bp_plan->SetAutoContinue(true);
588 }
589 step_bp_plan_sp.reset (step_bp_plan);
590 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000591 }
Chris Lattner24943d22010-06-08 16:52:24 +0000592 }
593 }
594 }
595 }
596}
597
598bool
Greg Claytona46013b2013-05-01 21:54:04 +0000599Thread::ShouldResume (StateType resume_state)
Chris Lattner24943d22010-06-08 16:52:24 +0000600{
601 // At this point clear the completed plan stack.
602 m_completed_plan_stack.clear();
603 m_discarded_plan_stack.clear();
Jim Ingham7c79a272013-05-08 00:35:16 +0000604 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner24943d22010-06-08 16:52:24 +0000605
Greg Clayton3acaa922012-09-25 02:40:06 +0000606 m_temporary_resume_state = resume_state;
Jim Ingham149d1f52012-01-31 23:09:20 +0000607
Greg Clayton863aa282013-05-09 01:55:29 +0000608 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
609 if (backing_thread_sp)
610 backing_thread_sp->m_temporary_resume_state = resume_state;
611
612 // Make sure m_stop_info_sp is valid
613 GetPrivateStopInfo();
Greg Claytona46013b2013-05-01 21:54:04 +0000614
Jim Ingham149d1f52012-01-31 23:09:20 +0000615 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
616 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
617 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
618 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000619 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton863aa282013-05-09 01:55:29 +0000620 if (m_stop_info_stop_id == process_stop_id &&
621 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham149d1f52012-01-31 23:09:20 +0000622 {
Greg Clayton863aa282013-05-09 01:55:29 +0000623 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham149d1f52012-01-31 23:09:20 +0000624 if (stop_info)
625 stop_info->WillResume (resume_state);
626 }
Chris Lattner24943d22010-06-08 16:52:24 +0000627
628 // Tell all the plans that we are about to resume in case they need to clear any state.
629 // We distinguish between the plan on the top of the stack and the lower
630 // plans in case a plan needs to do any special business before it runs.
631
Greg Clayton5042acf2013-04-20 00:27:58 +0000632 bool need_to_resume = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000633 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Clayton5042acf2013-04-20 00:27:58 +0000634 if (plan_ptr)
635 {
636 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000637
Greg Clayton5042acf2013-04-20 00:27:58 +0000638 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
639 {
640 plan_ptr->WillResume (resume_state, false);
641 }
642
643 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
644 // In that case, don't reset it here.
645
646 if (need_to_resume && resume_state != eStateSuspended)
647 {
Greg Clayton863aa282013-05-09 01:55:29 +0000648 m_stop_info_sp.reset();
Greg Clayton5042acf2013-04-20 00:27:58 +0000649 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000650 }
651
Greg Claytona46013b2013-05-01 21:54:04 +0000652 if (need_to_resume)
653 {
654 ClearStackFrames();
655 // Let Thread subclasses do any special work they need to prior to resuming
656 WillResume (resume_state);
657 }
658
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000659 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000660}
661
662void
663Thread::DidResume ()
664{
665 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
666}
667
Andrew Kaylor1f85fa82013-05-10 17:19:04 +0000668void
669Thread::DidStop ()
670{
671 SetState (eStateStopped);
672}
673
Chris Lattner24943d22010-06-08 16:52:24 +0000674bool
675Thread::ShouldStop (Event* event_ptr)
676{
677 ThreadPlan *current_plan = GetCurrentPlan();
Greg Clayton5042acf2013-04-20 00:27:58 +0000678
Chris Lattner24943d22010-06-08 16:52:24 +0000679 bool should_stop = true;
680
Greg Clayton952e9dc2013-03-27 23:08:40 +0000681 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000682
683 if (GetResumeState () == eStateSuspended)
684 {
685 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000686 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham149d1f52012-01-31 23:09:20 +0000687 __FUNCTION__,
Greg Claytona46013b2013-05-01 21:54:04 +0000688 GetID (),
689 GetProtocolID());
Jim Ingham149d1f52012-01-31 23:09:20 +0000690 return false;
691 }
692
693 if (GetTemporaryResumeState () == eStateSuspended)
694 {
695 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000696 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham149d1f52012-01-31 23:09:20 +0000697 __FUNCTION__,
Greg Claytona46013b2013-05-01 21:54:04 +0000698 GetID (),
699 GetProtocolID());
Jim Ingham149d1f52012-01-31 23:09:20 +0000700 return false;
701 }
702
Daniel Malea375ba882013-05-14 15:20:12 +0000703 // Based on the current thread plan and process stop info, check if this
704 // thread caused the process to stop. NOTE: this must take place before
705 // the plan is moved from the current plan stack to the completed plan
706 // stack.
Jim Ingham149d1f52012-01-31 23:09:20 +0000707 if (ThreadStoppedForAReason() == false)
708 {
709 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000710 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 Ingham149d1f52012-01-31 23:09:20 +0000711 __FUNCTION__,
Greg Claytona46013b2013-05-01 21:54:04 +0000712 GetID (),
713 GetProtocolID(),
Greg Clayton4ee040c2013-04-02 20:32:37 +0000714 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham149d1f52012-01-31 23:09:20 +0000715 return false;
716 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000717
Chris Lattner24943d22010-06-08 16:52:24 +0000718 if (log)
719 {
Jim Inghamc4353f22013-06-22 00:27:45 +0000720 log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
721 __FUNCTION__,
722 this,
Greg Claytona46013b2013-05-01 21:54:04 +0000723 GetID (),
724 GetProtocolID (),
Greg Clayton4ee040c2013-04-02 20:32:37 +0000725 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Inghame8f4e112011-10-15 00:23:43 +0000726 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000727 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000728 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000729 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000730 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000731 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000732
733 // The top most plan always gets to do the trace log...
734 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000735
736 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
737 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
738 // do any more work on this stop.
Greg Clayton863aa282013-05-09 01:55:29 +0000739 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Inghame787c7e2012-04-20 21:16:56 +0000740 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
741 {
742 if (log)
743 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
744 return false;
745 }
Chris Lattner24943d22010-06-08 16:52:24 +0000746
Jim Ingham98d50212012-09-05 21:12:49 +0000747 // If we've already been restarted, don't query the plans since the state they would examine is not current.
748 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
749 return false;
750
751 // Before the plans see the state of the world, calculate the current inlined depth.
752 GetStackFrameList()->CalculateCurrentInlinedDepth();
753
Jim Inghamad382c52011-12-03 01:52:59 +0000754 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
755 // If that plan is still working, then we don't need to do any more work. If the plan that explains
756 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
757 // whether they still need to do more work.
758
759 bool done_processing_current_plan = false;
760
Jim Ingham89e248f2013-02-09 01:29:05 +0000761 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Inghamad382c52011-12-03 01:52:59 +0000762 {
763 if (current_plan->TracerExplainsStop())
764 {
765 done_processing_current_plan = true;
766 should_stop = false;
767 }
768 else
769 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000770 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000771 // does and let it handle the situation.
772 ThreadPlan *plan_ptr = current_plan;
773 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
774 {
Jim Ingham89e248f2013-02-09 01:29:05 +0000775 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Inghamad382c52011-12-03 01:52:59 +0000776 {
777 should_stop = plan_ptr->ShouldStop (event_ptr);
778
779 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
780 // and all the plans below it off the stack.
781
782 if (plan_ptr->MischiefManaged())
783 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000784 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000785 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000786
787 do
788 {
789 if (should_stop)
790 current_plan->WillStop();
791 PopPlan();
792 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000793 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
794 // Now, if the responsible plan was not "Okay to discard" then we're done,
795 // otherwise we forward this to the next plan in the stack below.
796 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
797 done_processing_current_plan = true;
798 else
799 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000800 }
801 else
802 done_processing_current_plan = true;
803
804 break;
805 }
806
807 }
808 }
809 }
810
811 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000812 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000813 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000814
Jim Inghame8f4e112011-10-15 00:23:43 +0000815 if (log)
816 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
817
Jim Inghamf9f40c22011-02-08 05:20:59 +0000818 // We're starting from the base plan, so just let it decide;
819 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000820 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000821 should_stop = current_plan->ShouldStop (event_ptr);
822 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000823 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000824 }
825 else
826 {
827 // Otherwise, don't let the base plan override what the other plans say to do, since
828 // presumably if there were other plans they would know what to do...
829 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000830 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000831 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000832 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000833
834 should_stop = current_plan->ShouldStop(event_ptr);
835 if (log)
836 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
837 if (current_plan->MischiefManaged())
838 {
839 if (should_stop)
840 current_plan->WillStop();
841
842 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
843 // Otherwise, see if the plan's parent wants to stop.
844
845 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
846 {
847 PopPlan();
848 break;
849 }
850 else
851 {
852
853 PopPlan();
854
855 current_plan = GetCurrentPlan();
856 if (current_plan == NULL)
857 {
858 break;
859 }
860 }
Chris Lattner24943d22010-06-08 16:52:24 +0000861 }
862 else
863 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000864 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000865 }
Chris Lattner24943d22010-06-08 16:52:24 +0000866 }
867 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000868
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000869 if (over_ride_stop)
870 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000871
872 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
873 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
874 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
875 // This code clears stale plans off the stack.
876
877 if (should_stop)
878 {
879 ThreadPlan *plan_ptr = GetCurrentPlan();
880 while (!PlanIsBasePlan(plan_ptr))
881 {
882 bool stale = plan_ptr->IsPlanStale ();
883 ThreadPlan *examined_plan = plan_ptr;
884 plan_ptr = GetPreviousPlan (examined_plan);
885
886 if (stale)
887 {
888 if (log)
889 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
890 DiscardThreadPlansUpToPlan(examined_plan);
891 }
892 }
893 }
894
Chris Lattner24943d22010-06-08 16:52:24 +0000895 }
Chris Lattner24943d22010-06-08 16:52:24 +0000896
Jim Inghame8f4e112011-10-15 00:23:43 +0000897 if (log)
898 {
899 StreamString s;
900 s.IndentMore();
901 DumpThreadPlans(&s);
902 log->Printf ("Plan stack final state:\n%s", s.GetData());
903 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
904 }
Chris Lattner24943d22010-06-08 16:52:24 +0000905 return should_stop;
906}
907
908Vote
909Thread::ShouldReportStop (Event* event_ptr)
910{
911 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000912 StateType temp_thread_state = GetTemporaryResumeState();
913
Greg Clayton952e9dc2013-03-27 23:08:40 +0000914 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000915
916 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
917 {
918 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000919 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)", GetID(), eVoteNoOpinion);
Chris Lattner24943d22010-06-08 16:52:24 +0000920 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000921 }
Chris Lattner24943d22010-06-08 16:52:24 +0000922
Jim Ingham149d1f52012-01-31 23:09:20 +0000923 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
924 {
925 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000926 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)", GetID(), eVoteNoOpinion);
Jim Ingham149d1f52012-01-31 23:09:20 +0000927 return eVoteNoOpinion;
928 }
929
930 if (!ThreadStoppedForAReason())
931 {
932 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000933 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)", GetID(), eVoteNoOpinion);
Jim Ingham149d1f52012-01-31 23:09:20 +0000934 return eVoteNoOpinion;
935 }
936
Chris Lattner24943d22010-06-08 16:52:24 +0000937 if (m_completed_plan_stack.size() > 0)
938 {
939 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000940 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000941 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000942 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
943 }
944 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000945 {
Jim Ingham89e248f2013-02-09 01:29:05 +0000946 Vote thread_vote = eVoteNoOpinion;
947 ThreadPlan *plan_ptr = GetCurrentPlan();
948 while (1)
949 {
950 if (plan_ptr->PlanExplainsStop(event_ptr))
951 {
952 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
953 break;
954 }
955 if (PlanIsBasePlan(plan_ptr))
956 break;
957 else
958 plan_ptr = GetPreviousPlan(plan_ptr);
959 }
Greg Clayton5205f0b2010-09-03 17:10:42 +0000960 if (log)
Greg Claytona46013b2013-05-01 21:54:04 +0000961 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan", GetID(), thread_vote);
Jim Ingham89e248f2013-02-09 01:29:05 +0000962
963 return thread_vote;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000964 }
Chris Lattner24943d22010-06-08 16:52:24 +0000965}
966
967Vote
968Thread::ShouldReportRun (Event* event_ptr)
969{
970 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000971
Chris Lattner24943d22010-06-08 16:52:24 +0000972 if (thread_state == eStateSuspended
973 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000974 {
Chris Lattner24943d22010-06-08 16:52:24 +0000975 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000976 }
977
Greg Clayton952e9dc2013-03-27 23:08:40 +0000978 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000979 if (m_completed_plan_stack.size() > 0)
980 {
981 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000982 if (log)
Jim Inghamc4353f22013-06-22 00:27:45 +0000983 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
984 GetIndexID(),
985 this,
Jim Inghamac959662011-01-24 06:34:17 +0000986 GetID(),
Greg Claytona46013b2013-05-01 21:54:04 +0000987 StateAsCString(GetTemporaryResumeState()),
Jim Inghamac959662011-01-24 06:34:17 +0000988 m_completed_plan_stack.back()->GetName());
989
Chris Lattner24943d22010-06-08 16:52:24 +0000990 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
991 }
992 else
Jim Inghamac959662011-01-24 06:34:17 +0000993 {
994 if (log)
Jim Inghamc4353f22013-06-22 00:27:45 +0000995 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
996 GetIndexID(),
997 this,
Jim Inghamac959662011-01-24 06:34:17 +0000998 GetID(),
Greg Claytona46013b2013-05-01 21:54:04 +0000999 StateAsCString(GetTemporaryResumeState()),
Jim Inghamac959662011-01-24 06:34:17 +00001000 GetCurrentPlan()->GetName());
1001
Chris Lattner24943d22010-06-08 16:52:24 +00001002 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +00001003 }
Chris Lattner24943d22010-06-08 16:52:24 +00001004}
1005
Jim Ingham3c7b5b92010-06-16 02:00:15 +00001006bool
1007Thread::MatchesSpec (const ThreadSpec *spec)
1008{
1009 if (spec == NULL)
1010 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +00001011
Jim Inghama2664912012-03-07 22:03:04 +00001012 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +00001013}
1014
Chris Lattner24943d22010-06-08 16:52:24 +00001015void
1016Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1017{
1018 if (thread_plan_sp)
1019 {
Jim Ingham745ac7a2010-11-11 19:26:09 +00001020 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1021 if (!thread_plan_sp->GetThreadPlanTracer())
1022 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +00001023 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001024
Chris Lattner24943d22010-06-08 16:52:24 +00001025 thread_plan_sp->DidPush();
1026
Greg Clayton952e9dc2013-03-27 23:08:40 +00001027 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001028 if (log)
1029 {
1030 StreamString s;
1031 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamc4353f22013-06-22 00:27:45 +00001032 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1033 this,
Chris Lattner24943d22010-06-08 16:52:24 +00001034 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +00001035 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00001036 }
1037 }
1038}
1039
1040void
1041Thread::PopPlan ()
1042{
Greg Clayton952e9dc2013-03-27 23:08:40 +00001043 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001044
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001045 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +00001046 return;
1047 else
1048 {
1049 ThreadPlanSP &plan = m_plan_stack.back();
1050 if (log)
1051 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001052 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00001053 }
1054 m_completed_plan_stack.push_back (plan);
1055 plan->WillPop();
1056 m_plan_stack.pop_back();
1057 }
1058}
1059
1060void
1061Thread::DiscardPlan ()
1062{
Jim Inghamc4353f22013-06-22 00:27:45 +00001063 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001064 if (m_plan_stack.size() > 1)
1065 {
1066 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamc4353f22013-06-22 00:27:45 +00001067 if (log)
1068 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1069
Chris Lattner24943d22010-06-08 16:52:24 +00001070 m_discarded_plan_stack.push_back (plan);
1071 plan->WillPop();
1072 m_plan_stack.pop_back();
1073 }
1074}
1075
1076ThreadPlan *
1077Thread::GetCurrentPlan ()
1078{
Jim Ingham2f41d272012-04-19 00:17:05 +00001079 // There will always be at least the base plan. If somebody is mucking with a
1080 // thread with an empty plan stack, we should assert right away.
Greg Clayton5042acf2013-04-20 00:27:58 +00001081 if (m_plan_stack.empty())
1082 return NULL;
Jim Ingham2f41d272012-04-19 00:17:05 +00001083 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001084}
1085
1086ThreadPlanSP
1087Thread::GetCompletedPlan ()
1088{
1089 ThreadPlanSP empty_plan_sp;
1090 if (!m_completed_plan_stack.empty())
1091 {
1092 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1093 {
1094 ThreadPlanSP completed_plan_sp;
1095 completed_plan_sp = m_completed_plan_stack[i];
1096 if (!completed_plan_sp->GetPrivate ())
1097 return completed_plan_sp;
1098 }
1099 }
1100 return empty_plan_sp;
1101}
1102
Jim Ingham1586d972011-12-17 01:35:57 +00001103ValueObjectSP
1104Thread::GetReturnValueObject ()
1105{
1106 if (!m_completed_plan_stack.empty())
1107 {
1108 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1109 {
1110 ValueObjectSP return_valobj_sp;
1111 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1112 if (return_valobj_sp)
1113 return return_valobj_sp;
1114 }
1115 }
1116 return ValueObjectSP();
1117}
1118
Chris Lattner24943d22010-06-08 16:52:24 +00001119bool
1120Thread::IsThreadPlanDone (ThreadPlan *plan)
1121{
Chris Lattner24943d22010-06-08 16:52:24 +00001122 if (!m_completed_plan_stack.empty())
1123 {
1124 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1125 {
1126 if (m_completed_plan_stack[i].get() == plan)
1127 return true;
1128 }
1129 }
1130 return false;
1131}
1132
1133bool
1134Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1135{
Chris Lattner24943d22010-06-08 16:52:24 +00001136 if (!m_discarded_plan_stack.empty())
1137 {
1138 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1139 {
1140 if (m_discarded_plan_stack[i].get() == plan)
1141 return true;
1142 }
1143 }
1144 return false;
1145}
1146
1147ThreadPlan *
1148Thread::GetPreviousPlan (ThreadPlan *current_plan)
1149{
1150 if (current_plan == NULL)
1151 return NULL;
1152
1153 int stack_size = m_completed_plan_stack.size();
1154 for (int i = stack_size - 1; i > 0; i--)
1155 {
1156 if (current_plan == m_completed_plan_stack[i].get())
1157 return m_completed_plan_stack[i-1].get();
1158 }
1159
1160 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1161 {
Chris Lattner24943d22010-06-08 16:52:24 +00001162 if (m_plan_stack.size() > 0)
1163 return m_plan_stack.back().get();
1164 else
1165 return NULL;
1166 }
1167
1168 stack_size = m_plan_stack.size();
1169 for (int i = stack_size - 1; i > 0; i--)
1170 {
1171 if (current_plan == m_plan_stack[i].get())
1172 return m_plan_stack[i-1].get();
1173 }
1174 return NULL;
1175}
1176
1177void
1178Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1179{
1180 if (abort_other_plans)
1181 DiscardThreadPlans(true);
1182
1183 PushPlan (thread_plan_sp);
1184}
1185
Jim Ingham745ac7a2010-11-11 19:26:09 +00001186
1187void
1188Thread::EnableTracer (bool value, bool single_stepping)
1189{
1190 int stack_size = m_plan_stack.size();
1191 for (int i = 0; i < stack_size; i++)
1192 {
1193 if (m_plan_stack[i]->GetThreadPlanTracer())
1194 {
1195 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1196 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1197 }
1198 }
1199}
1200
1201void
1202Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1203{
1204 int stack_size = m_plan_stack.size();
1205 for (int i = 0; i < stack_size; i++)
1206 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1207}
1208
Chris Lattner24943d22010-06-08 16:52:24 +00001209void
Jim Inghamea9d4262010-11-05 19:25:48 +00001210Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1211{
Jim Ingham88e3de22012-05-03 21:19:36 +00001212 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1213}
1214
1215void
1216Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1217{
Greg Clayton952e9dc2013-03-27 23:08:40 +00001218 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +00001219 if (log)
1220 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001221 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", GetID(), up_to_plan_ptr);
Jim Inghamea9d4262010-11-05 19:25:48 +00001222 }
1223
1224 int stack_size = m_plan_stack.size();
1225
1226 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1227 // stack, and if so discard up to and including it.
1228
Jim Ingham88e3de22012-05-03 21:19:36 +00001229 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +00001230 {
1231 for (int i = stack_size - 1; i > 0; i--)
1232 DiscardPlan();
1233 }
1234 else
1235 {
1236 bool found_it = false;
1237 for (int i = stack_size - 1; i > 0; i--)
1238 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001239 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001240 found_it = true;
1241 }
1242 if (found_it)
1243 {
1244 bool last_one = false;
1245 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1246 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001247 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001248 last_one = true;
1249 DiscardPlan();
1250 }
1251 }
1252 }
1253 return;
1254}
1255
1256void
Chris Lattner24943d22010-06-08 16:52:24 +00001257Thread::DiscardThreadPlans(bool force)
1258{
Greg Clayton952e9dc2013-03-27 23:08:40 +00001259 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001260 if (log)
1261 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001262 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +00001263 }
1264
1265 if (force)
1266 {
1267 int stack_size = m_plan_stack.size();
1268 for (int i = stack_size - 1; i > 0; i--)
1269 {
1270 DiscardPlan();
1271 }
1272 return;
1273 }
1274
1275 while (1)
1276 {
1277
1278 int master_plan_idx;
Jim Inghamf1dbb712012-09-11 00:09:25 +00001279 bool discard = true;
Chris Lattner24943d22010-06-08 16:52:24 +00001280
1281 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1282 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1283 {
1284 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1285 {
1286 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1287 break;
1288 }
1289 }
1290
1291 if (discard)
1292 {
1293 // First pop all the dependent plans:
1294 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1295 {
1296
1297 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1298 // for the plan leaves it in a state that it is safe to pop the plan
1299 // with no more notice?
1300 DiscardPlan();
1301 }
1302
1303 // Now discard the master plan itself.
1304 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1305 // discard it's dependent plans, but not it...
1306 if (master_plan_idx > 0)
1307 {
1308 DiscardPlan();
1309 }
1310 }
1311 else
1312 {
1313 // If the master plan doesn't want to get discarded, then we're done.
1314 break;
1315 }
1316
1317 }
Chris Lattner24943d22010-06-08 16:52:24 +00001318}
1319
Jim Ingham2bcbaf62012-04-09 22:37:39 +00001320bool
1321Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1322{
1323 if (plan_ptr->IsBasePlan())
1324 return true;
1325 else if (m_plan_stack.size() == 0)
1326 return false;
1327 else
1328 return m_plan_stack[0].get() == plan_ptr;
1329}
1330
Jim Inghambe51f8a2013-01-31 21:46:01 +00001331Error
1332Thread::UnwindInnermostExpression()
1333{
1334 Error error;
1335 int stack_size = m_plan_stack.size();
1336
1337 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1338 // stack, and if so discard up to and including it.
1339
1340 for (int i = stack_size - 1; i > 0; i--)
1341 {
1342 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1343 {
1344 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1345 return error;
1346 }
1347 }
1348 error.SetErrorString("No expressions currently active on this thread");
1349 return error;
1350}
1351
1352
Jim Ingham008f3dc2013-07-18 21:48:26 +00001353ThreadPlanSP
Chris Lattner24943d22010-06-08 16:52:24 +00001354Thread::QueueFundamentalPlan (bool abort_other_plans)
1355{
1356 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1357 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001358 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001359}
1360
Jim Ingham008f3dc2013-07-18 21:48:26 +00001361ThreadPlanSP
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001362Thread::QueueThreadPlanForStepSingleInstruction
1363(
1364 bool step_over,
1365 bool abort_other_plans,
1366 bool stop_other_threads
1367)
Chris Lattner24943d22010-06-08 16:52:24 +00001368{
1369 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1370 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001371 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001372}
1373
Jim Ingham008f3dc2013-07-18 21:48:26 +00001374ThreadPlanSP
Jim Inghamf2ca5732012-12-12 19:58:40 +00001375Thread::QueueThreadPlanForStepOverRange
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001376(
1377 bool abort_other_plans,
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001378 const AddressRange &range,
Jim Inghamf2ca5732012-12-12 19:58:40 +00001379 const SymbolContext &addr_context,
1380 lldb::RunMode stop_other_threads
1381)
1382{
1383 ThreadPlanSP thread_plan_sp;
1384 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1385
1386 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001387 return thread_plan_sp;
Jim Inghamf2ca5732012-12-12 19:58:40 +00001388}
1389
Jim Ingham008f3dc2013-07-18 21:48:26 +00001390ThreadPlanSP
Jim Inghamf2ca5732012-12-12 19:58:40 +00001391Thread::QueueThreadPlanForStepInRange
1392(
1393 bool abort_other_plans,
1394 const AddressRange &range,
1395 const SymbolContext &addr_context,
1396 const char *step_in_target,
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001397 lldb::RunMode stop_other_threads,
1398 bool avoid_code_without_debug_info
1399)
Chris Lattner24943d22010-06-08 16:52:24 +00001400{
1401 ThreadPlanSP thread_plan_sp;
Jim Inghamf2ca5732012-12-12 19:58:40 +00001402 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1403 if (avoid_code_without_debug_info)
1404 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner24943d22010-06-08 16:52:24 +00001405 else
Jim Inghamf2ca5732012-12-12 19:58:40 +00001406 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1407 if (step_in_target)
1408 plan->SetStepInTarget(step_in_target);
1409 thread_plan_sp.reset (plan);
Chris Lattner24943d22010-06-08 16:52:24 +00001410
1411 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001412 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001413}
1414
1415
Jim Ingham008f3dc2013-07-18 21:48:26 +00001416ThreadPlanSP
Chris Lattner24943d22010-06-08 16:52:24 +00001417Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1418{
1419 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1420 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001421 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001422}
1423
Jim Ingham008f3dc2013-07-18 21:48:26 +00001424ThreadPlanSP
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001425Thread::QueueThreadPlanForStepOut
1426(
1427 bool abort_other_plans,
1428 SymbolContext *addr_context,
1429 bool first_insn,
1430 bool stop_other_threads,
1431 Vote stop_vote,
1432 Vote run_vote,
1433 uint32_t frame_idx
1434)
Chris Lattner24943d22010-06-08 16:52:24 +00001435{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001436 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1437 addr_context,
1438 first_insn,
1439 stop_other_threads,
1440 stop_vote,
1441 run_vote,
1442 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001443
1444 if (thread_plan_sp->ValidatePlan(NULL))
1445 {
1446 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001447 return thread_plan_sp;
Sean Callananf6d5fea2012-07-31 22:19:25 +00001448 }
1449 else
1450 {
Jim Ingham008f3dc2013-07-18 21:48:26 +00001451 return ThreadPlanSP();
Sean Callananf6d5fea2012-07-31 22:19:25 +00001452 }
Chris Lattner24943d22010-06-08 16:52:24 +00001453}
1454
Jim Ingham008f3dc2013-07-18 21:48:26 +00001455ThreadPlanSP
Jim Ingham038fa8e2012-05-10 01:35:39 +00001456Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001457{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001458 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001459 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham008f3dc2013-07-18 21:48:26 +00001460 return ThreadPlanSP();
Jim Inghamad382c52011-12-03 01:52:59 +00001461
Chris Lattner24943d22010-06-08 16:52:24 +00001462 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001463 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001464}
1465
Jim Ingham008f3dc2013-07-18 21:48:26 +00001466ThreadPlanSP
Chris Lattner24943d22010-06-08 16:52:24 +00001467Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1468 Address& function,
1469 lldb::addr_t arg,
1470 bool stop_other_threads,
Jim Inghamb7940202013-01-15 02:47:48 +00001471 bool unwind_on_error,
1472 bool ignore_breakpoints)
Chris Lattner24943d22010-06-08 16:52:24 +00001473{
Jim Inghamb7940202013-01-15 02:47:48 +00001474 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1475 function,
1476 ClangASTType(),
1477 arg,
1478 stop_other_threads,
1479 unwind_on_error,
1480 ignore_breakpoints));
Chris Lattner24943d22010-06-08 16:52:24 +00001481 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001482 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001483}
1484
Jim Ingham008f3dc2013-07-18 21:48:26 +00001485ThreadPlanSP
Chris Lattner24943d22010-06-08 16:52:24 +00001486Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1487 Address &target_addr,
1488 bool stop_other_threads)
1489{
1490 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1491 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001492 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001493}
1494
Jim Ingham008f3dc2013-07-18 21:48:26 +00001495ThreadPlanSP
Chris Lattner24943d22010-06-08 16:52:24 +00001496Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001497 lldb::addr_t *address_list,
1498 size_t num_addresses,
1499 bool stop_other_threads,
1500 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001501{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001502 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001503 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham008f3dc2013-07-18 21:48:26 +00001504 return thread_plan_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001505
1506}
1507
1508uint32_t
1509Thread::GetIndexID () const
1510{
1511 return m_index_id;
1512}
1513
1514void
1515Thread::DumpThreadPlans (lldb_private::Stream *s) const
1516{
1517 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001518 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001519 s->Indent();
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001520 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4" PRIx64 ", stack_size = %d\n", GetIndexID(), GetID(), stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +00001521 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001522 {
Chris Lattner24943d22010-06-08 16:52:24 +00001523 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001524 s->Indent();
1525 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001526 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001527 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001528 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001529 }
1530
Chris Lattner24943d22010-06-08 16:52:24 +00001531 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001532 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001533 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001534 s->Indent();
1535 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1536 for (i = stack_size - 1; i >= 0; i--)
1537 {
1538 s->IndentMore();
1539 s->Indent();
1540 s->Printf ("Element %d: ", i);
1541 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1542 s->EOL();
1543 s->IndentLess();
1544 }
Chris Lattner24943d22010-06-08 16:52:24 +00001545 }
1546
1547 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001548 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001549 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001550 s->Indent();
1551 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1552 for (i = stack_size - 1; i >= 0; i--)
1553 {
1554 s->IndentMore();
1555 s->Indent();
1556 s->Printf ("Element %d: ", i);
1557 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1558 s->EOL();
1559 s->IndentLess();
1560 }
Chris Lattner24943d22010-06-08 16:52:24 +00001561 }
1562
1563}
1564
Greg Clayton289afcb2012-02-18 05:35:26 +00001565TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001566Thread::CalculateTarget ()
1567{
Greg Claytonf4124de2012-02-21 00:09:25 +00001568 TargetSP target_sp;
1569 ProcessSP process_sp(GetProcess());
1570 if (process_sp)
1571 target_sp = process_sp->CalculateTarget();
1572 return target_sp;
1573
Chris Lattner24943d22010-06-08 16:52:24 +00001574}
1575
Greg Clayton289afcb2012-02-18 05:35:26 +00001576ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001577Thread::CalculateProcess ()
1578{
Greg Claytonf4124de2012-02-21 00:09:25 +00001579 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001580}
1581
Greg Clayton289afcb2012-02-18 05:35:26 +00001582ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001583Thread::CalculateThread ()
1584{
Greg Clayton289afcb2012-02-18 05:35:26 +00001585 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001586}
1587
Greg Clayton289afcb2012-02-18 05:35:26 +00001588StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001589Thread::CalculateStackFrame ()
1590{
Greg Clayton289afcb2012-02-18 05:35:26 +00001591 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001592}
1593
1594void
Greg Claytona830adb2010-10-04 01:05:56 +00001595Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001596{
Greg Claytonf4124de2012-02-21 00:09:25 +00001597 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001598}
1599
Greg Clayton782b9cc2010-08-25 00:35:26 +00001600
Greg Clayton2450cb12012-03-29 01:41:38 +00001601StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001602Thread::GetStackFrameList ()
1603{
Greg Clayton2450cb12012-03-29 01:41:38 +00001604 StackFrameListSP frame_list_sp;
1605 Mutex::Locker locker(m_frame_mutex);
1606 if (m_curr_frames_sp)
1607 {
1608 frame_list_sp = m_curr_frames_sp;
1609 }
1610 else
1611 {
1612 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1613 m_curr_frames_sp = frame_list_sp;
1614 }
1615 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001616}
1617
Greg Clayton782b9cc2010-08-25 00:35:26 +00001618void
1619Thread::ClearStackFrames ()
1620{
Greg Clayton2450cb12012-03-29 01:41:38 +00001621 Mutex::Locker locker(m_frame_mutex);
1622
Greg Claytona46013b2013-05-01 21:54:04 +00001623 Unwind *unwinder = GetUnwinder ();
1624 if (unwinder)
1625 unwinder->Clear();
1626
Jim Inghambf97d742012-02-29 03:40:22 +00001627 // Only store away the old "reference" StackFrameList if we got all its frames:
1628 // FIXME: At some point we can try to splice in the frames we have fetched into
1629 // the new frame as we make it, but let's not try that now.
1630 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001631 m_prev_frames_sp.swap (m_curr_frames_sp);
1632 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001633}
1634
1635lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001636Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1637{
Greg Clayton2450cb12012-03-29 01:41:38 +00001638 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001639}
1640
Jim Inghama17a81a2012-09-12 00:40:39 +00001641
1642Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001643Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001644{
1645 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1646 Error return_error;
1647
1648 if (!frame_sp)
1649 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001650 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Inghama17a81a2012-09-12 00:40:39 +00001651 }
1652
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001653 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Inghama17a81a2012-09-12 00:40:39 +00001654}
1655
1656Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001657Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001658{
1659 Error return_error;
1660
1661 if (!frame_sp)
1662 {
1663 return_error.SetErrorString("Can't return to a null frame.");
1664 return return_error;
1665 }
1666
1667 Thread *thread = frame_sp->GetThread().get();
Jim Inghamf59388a2012-09-14 02:14:15 +00001668 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1669 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Inghambe51f8a2013-01-31 21:46:01 +00001670 if (!older_frame_sp)
1671 {
1672 return_error.SetErrorString("No older frame to return to.");
1673 return return_error;
1674 }
Jim Inghama17a81a2012-09-12 00:40:39 +00001675
1676 if (return_value_sp)
Jim Ingham46657452012-09-27 01:15:29 +00001677 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001678 lldb::ABISP abi = thread->GetProcess()->GetABI();
1679 if (!abi)
1680 {
1681 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham6f01c932012-10-12 17:34:26 +00001682 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001683 }
Jim Ingham46657452012-09-27 01:15:29 +00001684 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1685
1686 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1687 // Turn that back on when that works.
1688 if (0 && sc.function != NULL)
1689 {
1690 Type *function_type = sc.function->GetType();
1691 if (function_type)
1692 {
Greg Clayton52f79232013-07-11 22:46:58 +00001693 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham46657452012-09-27 01:15:29 +00001694 if (return_type)
1695 {
Jim Ingham46657452012-09-27 01:15:29 +00001696 StreamString s;
Greg Clayton52f79232013-07-11 22:46:58 +00001697 return_type.DumpTypeDescription(&s);
1698 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham46657452012-09-27 01:15:29 +00001699 if (cast_value_sp)
1700 {
1701 cast_value_sp->SetFormat(eFormatHex);
1702 return_value_sp = cast_value_sp;
1703 }
1704 }
1705 }
1706 }
1707
Jim Inghamf59388a2012-09-14 02:14:15 +00001708 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001709 if (!return_error.Success())
1710 return return_error;
1711 }
1712
1713 // Now write the return registers for the chosen frame:
Jim Inghamf59388a2012-09-14 02:14:15 +00001714 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Inghambe51f8a2013-01-31 21:46:01 +00001715 // cook their data
1716
1717 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1718 if (youngest_frame_sp)
Jim Inghama17a81a2012-09-12 00:40:39 +00001719 {
Greg Clayton4ee040c2013-04-02 20:32:37 +00001720 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1721 if (reg_ctx_sp)
Jim Inghambe51f8a2013-01-31 21:46:01 +00001722 {
Greg Clayton4ee040c2013-04-02 20:32:37 +00001723 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1724 if (copy_success)
1725 {
1726 thread->DiscardThreadPlans(true);
1727 thread->ClearStackFrames();
1728 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1729 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1730 }
1731 else
1732 {
1733 return_error.SetErrorString("Could not reset register values.");
1734 }
Jim Inghambe51f8a2013-01-31 21:46:01 +00001735 }
1736 else
1737 {
Greg Clayton4ee040c2013-04-02 20:32:37 +00001738 return_error.SetErrorString("Frame has no register context.");
Jim Inghambe51f8a2013-01-31 21:46:01 +00001739 }
Jim Inghama17a81a2012-09-12 00:40:39 +00001740 }
1741 else
1742 {
Jim Inghambe51f8a2013-01-31 21:46:01 +00001743 return_error.SetErrorString("Returned past top frame.");
Jim Inghama17a81a2012-09-12 00:40:39 +00001744 }
Greg Clayton9b769d32013-02-01 02:52:31 +00001745 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001746}
1747
Chris Lattner24943d22010-06-08 16:52:24 +00001748void
Greg Claytona830adb2010-10-04 01:05:56 +00001749Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001750{
Greg Claytonf4124de2012-02-21 00:09:25 +00001751 ExecutionContext exe_ctx (shared_from_this());
1752 Process *process = exe_ctx.GetProcessPtr();
1753 if (process == NULL)
1754 return;
1755
Greg Clayton567e7f32011-09-22 04:58:26 +00001756 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001757 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001758 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001759 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001760 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001761 if (frame_sp)
1762 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001763 exe_ctx.SetFrameSP(frame_sp);
1764 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001765 }
1766 }
1767
Greg Claytonf4124de2012-02-21 00:09:25 +00001768 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001769 assert (thread_format);
Greg Claytona830adb2010-10-04 01:05:56 +00001770 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001771 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001772 &exe_ctx,
1773 NULL,
Michael Sartain17d5a032013-05-23 20:47:45 +00001774 strm);
Chris Lattner24943d22010-06-08 16:52:24 +00001775}
1776
Greg Clayton990de7b2010-11-18 23:32:35 +00001777void
Caroline Tice2a456812011-03-10 22:14:10 +00001778Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001779{
Greg Clayton990de7b2010-11-18 23:32:35 +00001780}
Jim Ingham20594b12010-09-08 03:14:33 +00001781
Greg Clayton990de7b2010-11-18 23:32:35 +00001782void
Caroline Tice2a456812011-03-10 22:14:10 +00001783Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001784{
Greg Clayton990de7b2010-11-18 23:32:35 +00001785}
Jim Ingham20594b12010-09-08 03:14:33 +00001786
Jim Inghamccd584d2010-09-23 17:40:12 +00001787lldb::StackFrameSP
1788Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1789{
Greg Clayton2450cb12012-03-29 01:41:38 +00001790 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001791}
Caroline Tice7826c882010-10-26 03:11:13 +00001792
1793const char *
1794Thread::StopReasonAsCString (lldb::StopReason reason)
1795{
1796 switch (reason)
1797 {
Andrew Kaylor278f16e2012-12-20 23:08:03 +00001798 case eStopReasonInvalid: return "invalid";
1799 case eStopReasonNone: return "none";
1800 case eStopReasonTrace: return "trace";
1801 case eStopReasonBreakpoint: return "breakpoint";
1802 case eStopReasonWatchpoint: return "watchpoint";
1803 case eStopReasonSignal: return "signal";
1804 case eStopReasonException: return "exception";
1805 case eStopReasonExec: return "exec";
1806 case eStopReasonPlanComplete: return "plan complete";
1807 case eStopReasonThreadExiting: return "thread exiting";
Caroline Tice7826c882010-10-26 03:11:13 +00001808 }
1809
1810
1811 static char unknown_state_string[64];
1812 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1813 return unknown_state_string;
1814}
1815
1816const char *
1817Thread::RunModeAsCString (lldb::RunMode mode)
1818{
1819 switch (mode)
1820 {
1821 case eOnlyThisThread: return "only this thread";
1822 case eAllThreads: return "all threads";
1823 case eOnlyDuringStepping: return "only during stepping";
1824 }
1825
1826 static char unknown_state_string[64];
1827 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1828 return unknown_state_string;
1829}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001830
Greg Claytonabe0fed2011-04-18 08:33:37 +00001831size_t
1832Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1833{
Greg Claytonf4124de2012-02-21 00:09:25 +00001834 ExecutionContext exe_ctx (shared_from_this());
1835 Target *target = exe_ctx.GetTargetPtr();
1836 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001837 size_t num_frames_shown = 0;
1838 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001839 bool is_selected = false;
1840 if (process)
1841 {
1842 if (process->GetThreadList().GetSelectedThread().get() == this)
1843 is_selected = true;
1844 }
1845 strm.Printf("%c ", is_selected ? '*' : ' ');
1846 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001847 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001848 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001849 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001850 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001851 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1852 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1853 {
1854 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1855 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001856 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001857 }
1858
1859 DumpUsingSettingsFormat (strm, start_frame);
1860
1861 if (num_frames > 0)
1862 {
1863 strm.IndentMore();
1864
1865 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001866 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001867 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1868 start_frame,
1869 num_frames,
1870 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001871 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001872 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001873 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001874 }
1875 return num_frames_shown;
1876}
1877
1878size_t
1879Thread::GetStackFrameStatus (Stream& strm,
1880 uint32_t first_frame,
1881 uint32_t num_frames,
1882 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001883 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001884{
Greg Clayton2450cb12012-03-29 01:41:38 +00001885 return GetStackFrameList()->GetStatus (strm,
1886 first_frame,
1887 num_frames,
1888 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001889 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001890}
1891
Peter Collingbournee426d852011-06-03 20:40:54 +00001892bool
1893Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1894{
1895 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1896 if (frame_sp)
1897 {
1898 checkpoint.SetStackID(frame_sp->GetStackID());
Greg Clayton4ee040c2013-04-02 20:32:37 +00001899 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1900 if (reg_ctx_sp)
1901 return reg_ctx_sp->ReadAllRegisterValues (checkpoint.GetData());
Peter Collingbournee426d852011-06-03 20:40:54 +00001902 }
1903 return false;
1904}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001905
Peter Collingbournee426d852011-06-03 20:40:54 +00001906bool
1907Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1908{
Jim Inghama17a81a2012-09-12 00:40:39 +00001909 return ResetFrameZeroRegisters (checkpoint.GetData());
1910}
1911
1912bool
1913Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1914{
Peter Collingbournee426d852011-06-03 20:40:54 +00001915 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1916 if (frame_sp)
1917 {
Greg Clayton4ee040c2013-04-02 20:32:37 +00001918 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1919 if (reg_ctx_sp)
1920 {
1921 bool ret = reg_ctx_sp->WriteAllRegisterValues (register_data_sp);
Peter Collingbournee426d852011-06-03 20:40:54 +00001922
Greg Clayton4ee040c2013-04-02 20:32:37 +00001923 // Clear out all stack frames as our world just changed.
1924 ClearStackFrames();
1925 reg_ctx_sp->InvalidateIfNeeded(true);
1926 if (m_unwinder_ap.get())
1927 m_unwinder_ap->Clear();
1928 return ret;
1929 }
Peter Collingbournee426d852011-06-03 20:40:54 +00001930 }
1931 return false;
1932}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001933
Greg Clayton37f962e2011-08-22 02:49:39 +00001934Unwind *
1935Thread::GetUnwinder ()
1936{
1937 if (m_unwinder_ap.get() == NULL)
1938 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001939 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001940 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1941 switch (machine)
1942 {
1943 case llvm::Triple::x86_64:
1944 case llvm::Triple::x86:
1945 case llvm::Triple::arm:
1946 case llvm::Triple::thumb:
1947 m_unwinder_ap.reset (new UnwindLLDB (*this));
1948 break;
1949
1950 default:
1951 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1952 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1953 break;
1954 }
1955 }
1956 return m_unwinder_ap.get();
1957}
1958
1959
Greg Claytoncf5927e2012-05-18 02:38:05 +00001960void
1961Thread::Flush ()
1962{
1963 ClearStackFrames ();
1964 m_reg_context_sp.reset();
1965}
Jim Ingham6bc24c12012-10-16 00:09:33 +00001966
Filipe Cabecinhasb2921e22012-11-20 00:11:13 +00001967bool
Jim Ingham6bc24c12012-10-16 00:09:33 +00001968Thread::IsStillAtLastBreakpointHit ()
1969{
1970 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1971 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1972 // multithreaded programs.
Greg Clayton863aa282013-05-09 01:55:29 +00001973 if (m_stop_info_sp) {
1974 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham6bc24c12012-10-16 00:09:33 +00001975 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton863aa282013-05-09 01:55:29 +00001976 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton4ee040c2013-04-02 20:32:37 +00001977 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
1978 if (reg_ctx_sp)
1979 {
1980 lldb::addr_t pc = reg_ctx_sp->GetPC();
1981 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1982 if (bp_site_sp && value == bp_site_sp->GetID())
1983 return true;
1984 }
Jim Ingham6bc24c12012-10-16 00:09:33 +00001985 }
1986 }
1987 return false;
1988}