blob: 98c26019b4a14a2b93dde9234e558ae7ffc77992 [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
Jason Molendab57e4a12013-11-04 09:33:30 +0000214StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000215Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
216{
217 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000218 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000219 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 Clayton6e0ff1a2013-05-09 01:55:29 +0000246 m_stop_info_sp (),
247 m_stop_info_stop_id (0),
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000248 m_index_id (process.GetNextThreadIndexID(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),
Jim Ingham221d51c2013-05-08 00:35:16 +0000262 m_override_should_notify (eLazyBoolCalculate)
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{
Jim Inghamb1499242013-10-18 17:11:02 +0000285 // Tell any plans on the plan stacks that the thread is being destroyed since
286 // any plans that have a thread go away in the middle of might need
287 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000288 for (auto plan : m_plan_stack)
289 plan->ThreadDestroyed();
290
Jim Inghamb1499242013-10-18 17:11:02 +0000291 for (auto plan : m_discarded_plan_stack)
292 plan->ThreadDestroyed();
293
294 for (auto plan : m_completed_plan_stack)
295 plan->ThreadDestroyed();
296
Greg Clayton35a4cc52012-10-29 20:52:08 +0000297 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000298 m_plan_stack.clear();
299 m_discarded_plan_stack.clear();
300 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000301
302 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
303 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
304 // without checking first whether it is destroyed, they won't crash.
305 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
306 m_plan_stack.push_back (null_plan_sp);
307
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000308 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000309 m_reg_context_sp.reset();
310 m_unwinder_ap.reset();
311 Mutex::Locker locker(m_frame_mutex);
312 m_curr_frames_sp.reset();
313 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314}
315
Jim Ingham4f465cf2012-10-10 18:32:14 +0000316void
317Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
318{
319 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
320 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
321}
322
323uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000324Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000325{
326 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
327 if (broadcast)
328 BroadcastSelectedFrameChange(frame->GetStackID());
329 return ret_value;
330}
331
332bool
333Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
334{
Jason Molendab57e4a12013-11-04 09:33:30 +0000335 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000336 if (frame_sp)
337 {
338 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
339 if (broadcast)
340 BroadcastSelectedFrameChange(frame_sp->GetStackID());
341 return true;
342 }
343 else
344 return false;
345}
346
Jim Ingham93208b82013-01-31 21:46:01 +0000347bool
348Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
349{
350 const bool broadcast = true;
351 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
352 if (success)
353 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000354 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000355 if (frame_sp)
356 {
357 bool already_shown = false;
358 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
359 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
360 {
361 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
362 }
363
364 bool show_frame_info = true;
365 bool show_source = !already_shown;
366 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
367 }
368 return false;
369 }
370 else
371 return false;
372}
373
Jim Ingham4f465cf2012-10-10 18:32:14 +0000374
Jim Inghamb15bfc72010-10-20 00:39:53 +0000375lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000376Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377{
Greg Clayton6e10f142013-07-30 00:23:06 +0000378 if (m_destroy_called)
379 return m_stop_info_sp;
380
Jim Inghamb15bfc72010-10-20 00:39:53 +0000381 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000382 ProcessSP process_sp (GetProcess());
383 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000384 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000385 {
Jim Ingham73ca05a2011-12-17 01:35:57 +0000386 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000387 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000388 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000389 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000390 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
391 (m_stop_info_sp && m_stop_info_sp->IsValid())) // Stop info is valid, just return what we have
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000392 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000393 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000394 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000395 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000396 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000397 GetPrivateStopInfo ();
398 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000399 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000400 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401}
402
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000403lldb::StopInfoSP
404Thread::GetPrivateStopInfo ()
405{
Greg Clayton6e10f142013-07-30 00:23:06 +0000406 if (m_destroy_called)
407 return m_stop_info_sp;
408
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000409 ProcessSP process_sp (GetProcess());
410 if (process_sp)
411 {
Daniel Malea246cb612013-05-14 15:20:12 +0000412 const uint32_t process_stop_id = process_sp->GetStopID();
413 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000414 {
Daniel Malea246cb612013-05-14 15:20:12 +0000415 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000416 {
Daniel Malea246cb612013-05-14 15:20:12 +0000417 if (m_stop_info_sp->IsValid()
418 || IsStillAtLastBreakpointHit()
419 || GetCurrentPlan()->IsVirtualStep())
420 SetStopInfo (m_stop_info_sp);
421 else
422 m_stop_info_sp.reset();
423 }
424
425 if (!m_stop_info_sp)
426 {
427 if (CalculateStopInfo() == false)
428 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000429 }
430 }
431 }
432 return m_stop_info_sp;
433}
434
435
Greg Clayton97d5cf02012-09-25 02:40:06 +0000436lldb::StopReason
437Thread::GetStopReason()
438{
439 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
440 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000441 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000442 return eStopReasonNone;
443}
444
445
446
Jim Ingham77787032011-01-20 02:03:18 +0000447void
448Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
449{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000450 m_stop_info_sp = stop_info_sp;
451 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000452 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000453 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000454 // If we are overriding the ShouldReportStop, do that here:
455 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000456 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000457 }
458
Greg Clayton1ac04c32012-02-21 00:09:25 +0000459 ProcessSP process_sp (GetProcess());
460 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000461 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000462 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000463 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000464 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
465 if (log)
Matt Kopecef143712013-06-03 18:00:07 +0000466 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 Ingham77787032011-01-20 02:03:18 +0000467}
468
469void
Jim Ingham221d51c2013-05-08 00:35:16 +0000470Thread::SetShouldReportStop (Vote vote)
471{
472 if (vote == eVoteNoOpinion)
473 return;
474 else
475 {
476 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000477 if (m_stop_info_sp)
478 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000479 }
480}
481
482void
Jim Ingham77787032011-01-20 02:03:18 +0000483Thread::SetStopInfoToNothing()
484{
485 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
486 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
487 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
488}
489
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490bool
491Thread::ThreadStoppedForAReason (void)
492{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000493 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494}
495
Jim Ingham77787032011-01-20 02:03:18 +0000496bool
497Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
498{
499 if (!SaveFrameZeroState(saved_state.register_backup))
500 return false;
501
502 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000503 ProcessSP process_sp (GetProcess());
504 if (process_sp)
505 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000506 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
507
Jim Ingham77787032011-01-20 02:03:18 +0000508 return true;
509}
510
511bool
Jim Ingham8559a352012-11-26 23:52:18 +0000512Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000513{
514 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham8559a352012-11-26 23:52:18 +0000515 return true;
516}
517
518bool
519Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
520{
Jim Ingham0c270682011-01-25 02:47:23 +0000521 if (saved_state.stop_info_sp)
522 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000523 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000524 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000525 return true;
526}
527
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528StateType
529Thread::GetState() const
530{
531 // If any other threads access this we will need a mutex for it
532 Mutex::Locker locker(m_state_mutex);
533 return m_state;
534}
535
536void
537Thread::SetState(StateType state)
538{
539 Mutex::Locker locker(m_state_mutex);
540 m_state = state;
541}
542
543void
544Thread::WillStop()
545{
546 ThreadPlan *current_plan = GetCurrentPlan();
547
548 // FIXME: I may decide to disallow threads with no plans. In which
549 // case this should go to an assert.
550
551 if (!current_plan)
552 return;
553
554 current_plan->WillStop();
555}
556
557void
558Thread::SetupForResume ()
559{
560 if (GetResumeState() != eStateSuspended)
561 {
562
563 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
564 // telling the current plan it will resume, since we might change what the current
565 // plan is.
566
Greg Clayton1afa68e2013-04-02 20:32:37 +0000567// StopReason stop_reason = lldb::eStopReasonInvalid;
568// StopInfoSP stop_info_sp = GetStopInfo();
569// if (stop_info_sp.get())
570// stop_reason = stop_info_sp->GetStopReason();
571// if (stop_reason == lldb::eStopReasonBreakpoint)
572 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
573 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000575 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
576 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000577 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000578 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
579 // special to step over a breakpoint.
580
581 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000582
Greg Clayton1afa68e2013-04-02 20:32:37 +0000583 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
584 {
585 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
586 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000588 ThreadPlanSP step_bp_plan_sp;
589 step_bp_plan->SetPrivate (true);
590
591 if (GetCurrentPlan()->RunState() != eStateStepping)
592 {
593 step_bp_plan->SetAutoContinue(true);
594 }
595 step_bp_plan_sp.reset (step_bp_plan);
596 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 }
599 }
600 }
601 }
602}
603
604bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000605Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606{
607 // At this point clear the completed plan stack.
608 m_completed_plan_stack.clear();
609 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000610 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611
Greg Clayton97d5cf02012-09-25 02:40:06 +0000612 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000613
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000614 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
615 if (backing_thread_sp)
616 backing_thread_sp->m_temporary_resume_state = resume_state;
617
618 // Make sure m_stop_info_sp is valid
619 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000620
Jim Ingham92087d82012-01-31 23:09:20 +0000621 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
622 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
623 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
624 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000625 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000626 if (m_stop_info_stop_id == process_stop_id &&
627 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000628 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000629 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000630 if (stop_info)
631 stop_info->WillResume (resume_state);
632 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633
634 // Tell all the plans that we are about to resume in case they need to clear any state.
635 // We distinguish between the plan on the top of the stack and the lower
636 // plans in case a plan needs to do any special business before it runs.
637
Greg Claytond1d06e42013-04-20 00:27:58 +0000638 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000640 if (plan_ptr)
641 {
642 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643
Greg Claytond1d06e42013-04-20 00:27:58 +0000644 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
645 {
646 plan_ptr->WillResume (resume_state, false);
647 }
648
649 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
650 // In that case, don't reset it here.
651
652 if (need_to_resume && resume_state != eStateSuspended)
653 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000654 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000655 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000656 }
657
Greg Clayton160c9d82013-05-01 21:54:04 +0000658 if (need_to_resume)
659 {
660 ClearStackFrames();
661 // Let Thread subclasses do any special work they need to prior to resuming
662 WillResume (resume_state);
663 }
664
Jim Ingham513c6bb2012-09-01 01:02:41 +0000665 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666}
667
668void
669Thread::DidResume ()
670{
671 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
672}
673
Andrew Kaylor29d65742013-05-10 17:19:04 +0000674void
675Thread::DidStop ()
676{
677 SetState (eStateStopped);
678}
679
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680bool
681Thread::ShouldStop (Event* event_ptr)
682{
683 ThreadPlan *current_plan = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000684
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 bool should_stop = true;
686
Greg Clayton5160ce52013-03-27 23:08:40 +0000687 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000688
689 if (GetResumeState () == eStateSuspended)
690 {
691 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000692 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 +0000693 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000694 GetID (),
695 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000696 return false;
697 }
698
699 if (GetTemporaryResumeState () == eStateSuspended)
700 {
701 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000702 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 +0000703 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000704 GetID (),
705 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000706 return false;
707 }
708
Daniel Malea246cb612013-05-14 15:20:12 +0000709 // Based on the current thread plan and process stop info, check if this
710 // thread caused the process to stop. NOTE: this must take place before
711 // the plan is moved from the current plan stack to the completed plan
712 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000713 if (ThreadStoppedForAReason() == false)
714 {
715 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000716 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 +0000717 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000718 GetID (),
719 GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000720 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000721 return false;
722 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000723
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 if (log)
725 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000726 log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
727 __FUNCTION__,
728 this,
Greg Clayton160c9d82013-05-01 21:54:04 +0000729 GetID (),
730 GetProtocolID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000731 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000732 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000734 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000736 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000738
739 // The top most plan always gets to do the trace log...
740 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000741
742 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
743 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
744 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000745 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000746 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
747 {
748 if (log)
749 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
750 return false;
751 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752
Jim Inghambad39e42012-09-05 21:12:49 +0000753 // If we've already been restarted, don't query the plans since the state they would examine is not current.
754 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
755 return false;
756
757 // Before the plans see the state of the world, calculate the current inlined depth.
758 GetStackFrameList()->CalculateCurrentInlinedDepth();
759
Jim Ingham25f66702011-12-03 01:52:59 +0000760 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
761 // If that plan is still working, then we don't need to do any more work. If the plan that explains
762 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
763 // whether they still need to do more work.
764
765 bool done_processing_current_plan = false;
766
Jim Ingham0161b492013-02-09 01:29:05 +0000767 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000768 {
769 if (current_plan->TracerExplainsStop())
770 {
771 done_processing_current_plan = true;
772 should_stop = false;
773 }
774 else
775 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000776 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000777 // does and let it handle the situation.
778 ThreadPlan *plan_ptr = current_plan;
779 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
780 {
Jim Ingham0161b492013-02-09 01:29:05 +0000781 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000782 {
783 should_stop = plan_ptr->ShouldStop (event_ptr);
784
785 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
786 // and all the plans below it off the stack.
787
788 if (plan_ptr->MischiefManaged())
789 {
Jim Ingham031177e2012-05-11 23:49:49 +0000790 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000791 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000792
793 do
794 {
795 if (should_stop)
796 current_plan->WillStop();
797 PopPlan();
798 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000799 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
800 // Now, if the responsible plan was not "Okay to discard" then we're done,
801 // otherwise we forward this to the next plan in the stack below.
802 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
803 done_processing_current_plan = true;
804 else
805 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000806 }
807 else
808 done_processing_current_plan = true;
809
810 break;
811 }
812
813 }
814 }
815 }
816
817 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000819 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000820
Jim Ingham10c4b242011-10-15 00:23:43 +0000821 if (log)
822 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
823
Jim Ingham0f16e732011-02-08 05:20:59 +0000824 // We're starting from the base plan, so just let it decide;
825 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000827 should_stop = current_plan->ShouldStop (event_ptr);
828 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000829 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000830 }
831 else
832 {
833 // Otherwise, don't let the base plan override what the other plans say to do, since
834 // presumably if there were other plans they would know what to do...
835 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000837 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000839
840 should_stop = current_plan->ShouldStop(event_ptr);
841 if (log)
842 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
843 if (current_plan->MischiefManaged())
844 {
845 if (should_stop)
846 current_plan->WillStop();
847
848 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
849 // Otherwise, see if the plan's parent wants to stop.
850
851 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
852 {
853 PopPlan();
854 break;
855 }
856 else
857 {
858
859 PopPlan();
860
861 current_plan = GetCurrentPlan();
862 if (current_plan == NULL)
863 {
864 break;
865 }
866 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 }
868 else
869 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000870 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 }
873 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000874
Jim Inghamb01e7422010-06-19 04:45:32 +0000875 if (over_ride_stop)
876 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000877
878 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
879 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
880 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
881 // This code clears stale plans off the stack.
882
883 if (should_stop)
884 {
885 ThreadPlan *plan_ptr = GetCurrentPlan();
886 while (!PlanIsBasePlan(plan_ptr))
887 {
888 bool stale = plan_ptr->IsPlanStale ();
889 ThreadPlan *examined_plan = plan_ptr;
890 plan_ptr = GetPreviousPlan (examined_plan);
891
892 if (stale)
893 {
894 if (log)
895 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
896 DiscardThreadPlansUpToPlan(examined_plan);
897 }
898 }
899 }
900
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902
Jim Ingham10c4b242011-10-15 00:23:43 +0000903 if (log)
904 {
905 StreamString s;
906 s.IndentMore();
907 DumpThreadPlans(&s);
908 log->Printf ("Plan stack final state:\n%s", s.GetData());
909 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 return should_stop;
912}
913
914Vote
915Thread::ShouldReportStop (Event* event_ptr)
916{
917 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000918 StateType temp_thread_state = GetTemporaryResumeState();
919
Greg Clayton5160ce52013-03-27 23:08:40 +0000920 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000921
922 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
923 {
924 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000925 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 +0000926 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000927 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928
Jim Ingham92087d82012-01-31 23:09:20 +0000929 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
930 {
931 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000932 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 +0000933 return eVoteNoOpinion;
934 }
935
936 if (!ThreadStoppedForAReason())
937 {
938 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000939 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 +0000940 return eVoteNoOpinion;
941 }
942
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 if (m_completed_plan_stack.size() > 0)
944 {
945 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000946 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000947 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 +0000948 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
949 }
950 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000951 {
Jim Ingham0161b492013-02-09 01:29:05 +0000952 Vote thread_vote = eVoteNoOpinion;
953 ThreadPlan *plan_ptr = GetCurrentPlan();
954 while (1)
955 {
956 if (plan_ptr->PlanExplainsStop(event_ptr))
957 {
958 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
959 break;
960 }
961 if (PlanIsBasePlan(plan_ptr))
962 break;
963 else
964 plan_ptr = GetPreviousPlan(plan_ptr);
965 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000966 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000967 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 +0000968
969 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000970 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971}
972
973Vote
974Thread::ShouldReportRun (Event* event_ptr)
975{
976 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000977
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978 if (thread_state == eStateSuspended
979 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000980 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000982 }
983
Greg Clayton5160ce52013-03-27 23:08:40 +0000984 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 if (m_completed_plan_stack.size() > 0)
986 {
987 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000988 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +0000989 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
990 GetIndexID(),
991 this,
Jim Ingham444586b2011-01-24 06:34:17 +0000992 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000993 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000994 m_completed_plan_stack.back()->GetName());
995
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
997 }
998 else
Jim Ingham444586b2011-01-24 06:34:17 +0000999 {
1000 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001001 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
1002 GetIndexID(),
1003 this,
Jim Ingham444586b2011-01-24 06:34:17 +00001004 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001005 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001006 GetCurrentPlan()->GetName());
1007
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001009 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010}
1011
Jim Ingham1b54c882010-06-16 02:00:15 +00001012bool
1013Thread::MatchesSpec (const ThreadSpec *spec)
1014{
1015 if (spec == NULL)
1016 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001017
Jim Ingham3d902922012-03-07 22:03:04 +00001018 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001019}
1020
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021void
1022Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1023{
1024 if (thread_plan_sp)
1025 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001026 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1027 if (!thread_plan_sp->GetThreadPlanTracer())
1028 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001029 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +00001030
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031 thread_plan_sp->DidPush();
1032
Greg Clayton5160ce52013-03-27 23:08:40 +00001033 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 if (log)
1035 {
1036 StreamString s;
1037 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001038 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1039 this,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001041 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 }
1043 }
1044}
1045
1046void
1047Thread::PopPlan ()
1048{
Greg Clayton5160ce52013-03-27 23:08:40 +00001049 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001051 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 return;
1053 else
1054 {
1055 ThreadPlanSP &plan = m_plan_stack.back();
1056 if (log)
1057 {
Daniel Malead01b2952012-11-29 21:49:15 +00001058 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059 }
1060 m_completed_plan_stack.push_back (plan);
1061 plan->WillPop();
1062 m_plan_stack.pop_back();
1063 }
1064}
1065
1066void
1067Thread::DiscardPlan ()
1068{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001069 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070 if (m_plan_stack.size() > 1)
1071 {
1072 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001073 if (log)
1074 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1075
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 m_discarded_plan_stack.push_back (plan);
1077 plan->WillPop();
1078 m_plan_stack.pop_back();
1079 }
1080}
1081
1082ThreadPlan *
1083Thread::GetCurrentPlan ()
1084{
Jim Inghame1471232012-04-19 00:17:05 +00001085 // There will always be at least the base plan. If somebody is mucking with a
1086 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001087 if (m_plan_stack.empty())
1088 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001089 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090}
1091
1092ThreadPlanSP
1093Thread::GetCompletedPlan ()
1094{
1095 ThreadPlanSP empty_plan_sp;
1096 if (!m_completed_plan_stack.empty())
1097 {
1098 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1099 {
1100 ThreadPlanSP completed_plan_sp;
1101 completed_plan_sp = m_completed_plan_stack[i];
1102 if (!completed_plan_sp->GetPrivate ())
1103 return completed_plan_sp;
1104 }
1105 }
1106 return empty_plan_sp;
1107}
1108
Jim Ingham73ca05a2011-12-17 01:35:57 +00001109ValueObjectSP
1110Thread::GetReturnValueObject ()
1111{
1112 if (!m_completed_plan_stack.empty())
1113 {
1114 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1115 {
1116 ValueObjectSP return_valobj_sp;
1117 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1118 if (return_valobj_sp)
1119 return return_valobj_sp;
1120 }
1121 }
1122 return ValueObjectSP();
1123}
1124
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125bool
1126Thread::IsThreadPlanDone (ThreadPlan *plan)
1127{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 if (!m_completed_plan_stack.empty())
1129 {
1130 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1131 {
1132 if (m_completed_plan_stack[i].get() == plan)
1133 return true;
1134 }
1135 }
1136 return false;
1137}
1138
1139bool
1140Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1141{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 if (!m_discarded_plan_stack.empty())
1143 {
1144 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1145 {
1146 if (m_discarded_plan_stack[i].get() == plan)
1147 return true;
1148 }
1149 }
1150 return false;
1151}
1152
1153ThreadPlan *
1154Thread::GetPreviousPlan (ThreadPlan *current_plan)
1155{
1156 if (current_plan == NULL)
1157 return NULL;
1158
1159 int stack_size = m_completed_plan_stack.size();
1160 for (int i = stack_size - 1; i > 0; i--)
1161 {
1162 if (current_plan == m_completed_plan_stack[i].get())
1163 return m_completed_plan_stack[i-1].get();
1164 }
1165
1166 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1167 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168 if (m_plan_stack.size() > 0)
1169 return m_plan_stack.back().get();
1170 else
1171 return NULL;
1172 }
1173
1174 stack_size = m_plan_stack.size();
1175 for (int i = stack_size - 1; i > 0; i--)
1176 {
1177 if (current_plan == m_plan_stack[i].get())
1178 return m_plan_stack[i-1].get();
1179 }
1180 return NULL;
1181}
1182
1183void
1184Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1185{
1186 if (abort_other_plans)
1187 DiscardThreadPlans(true);
1188
1189 PushPlan (thread_plan_sp);
1190}
1191
Jim Ingham06e827c2010-11-11 19:26:09 +00001192
1193void
1194Thread::EnableTracer (bool value, bool single_stepping)
1195{
1196 int stack_size = m_plan_stack.size();
1197 for (int i = 0; i < stack_size; i++)
1198 {
1199 if (m_plan_stack[i]->GetThreadPlanTracer())
1200 {
1201 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1202 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1203 }
1204 }
1205}
1206
1207void
1208Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1209{
1210 int stack_size = m_plan_stack.size();
1211 for (int i = 0; i < stack_size; i++)
1212 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1213}
1214
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001216Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1217{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001218 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1219}
1220
1221void
1222Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1223{
Greg Clayton5160ce52013-03-27 23:08:40 +00001224 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001225 if (log)
1226 {
Daniel Malead01b2952012-11-29 21:49:15 +00001227 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 +00001228 }
1229
1230 int stack_size = m_plan_stack.size();
1231
1232 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1233 // stack, and if so discard up to and including it.
1234
Jim Ingham64e7ead2012-05-03 21:19:36 +00001235 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001236 {
1237 for (int i = stack_size - 1; i > 0; i--)
1238 DiscardPlan();
1239 }
1240 else
1241 {
1242 bool found_it = false;
1243 for (int i = stack_size - 1; i > 0; i--)
1244 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001245 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001246 found_it = true;
1247 }
1248 if (found_it)
1249 {
1250 bool last_one = false;
1251 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1252 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001253 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001254 last_one = true;
1255 DiscardPlan();
1256 }
1257 }
1258 }
1259 return;
1260}
1261
1262void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263Thread::DiscardThreadPlans(bool force)
1264{
Greg Clayton5160ce52013-03-27 23:08:40 +00001265 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266 if (log)
1267 {
Daniel Malead01b2952012-11-29 21:49:15 +00001268 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269 }
1270
1271 if (force)
1272 {
1273 int stack_size = m_plan_stack.size();
1274 for (int i = stack_size - 1; i > 0; i--)
1275 {
1276 DiscardPlan();
1277 }
1278 return;
1279 }
1280
1281 while (1)
1282 {
1283
1284 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001285 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286
1287 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1288 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1289 {
1290 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1291 {
1292 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1293 break;
1294 }
1295 }
1296
1297 if (discard)
1298 {
1299 // First pop all the dependent plans:
1300 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1301 {
1302
1303 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1304 // for the plan leaves it in a state that it is safe to pop the plan
1305 // with no more notice?
1306 DiscardPlan();
1307 }
1308
1309 // Now discard the master plan itself.
1310 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1311 // discard it's dependent plans, but not it...
1312 if (master_plan_idx > 0)
1313 {
1314 DiscardPlan();
1315 }
1316 }
1317 else
1318 {
1319 // If the master plan doesn't want to get discarded, then we're done.
1320 break;
1321 }
1322
1323 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324}
1325
Jim Inghamcf274f92012-04-09 22:37:39 +00001326bool
1327Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1328{
1329 if (plan_ptr->IsBasePlan())
1330 return true;
1331 else if (m_plan_stack.size() == 0)
1332 return false;
1333 else
1334 return m_plan_stack[0].get() == plan_ptr;
1335}
1336
Jim Ingham93208b82013-01-31 21:46:01 +00001337Error
1338Thread::UnwindInnermostExpression()
1339{
1340 Error error;
1341 int stack_size = m_plan_stack.size();
1342
1343 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1344 // stack, and if so discard up to and including it.
1345
1346 for (int i = stack_size - 1; i > 0; i--)
1347 {
1348 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1349 {
1350 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1351 return error;
1352 }
1353 }
1354 error.SetErrorString("No expressions currently active on this thread");
1355 return error;
1356}
1357
1358
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001359ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360Thread::QueueFundamentalPlan (bool abort_other_plans)
1361{
1362 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1363 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001364 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365}
1366
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001367ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001368Thread::QueueThreadPlanForStepSingleInstruction
1369(
1370 bool step_over,
1371 bool abort_other_plans,
1372 bool stop_other_threads
1373)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374{
1375 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1376 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001377 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378}
1379
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001380ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001381Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001382(
1383 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001384 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001385 const SymbolContext &addr_context,
1386 lldb::RunMode stop_other_threads
1387)
1388{
1389 ThreadPlanSP thread_plan_sp;
1390 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1391
1392 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001393 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001394}
1395
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001396ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001397Thread::QueueThreadPlanForStepInRange
1398(
1399 bool abort_other_plans,
1400 const AddressRange &range,
1401 const SymbolContext &addr_context,
1402 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001403 lldb::RunMode stop_other_threads,
1404 bool avoid_code_without_debug_info
1405)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406{
1407 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001408 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1409 if (avoid_code_without_debug_info)
1410 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 else
Jim Inghamc6276822012-12-12 19:58:40 +00001412 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1413 if (step_in_target)
1414 plan->SetStepInTarget(step_in_target);
1415 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416
1417 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001418 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419}
1420
1421
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001422ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1424{
1425 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1426 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001427 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428}
1429
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001430ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001431Thread::QueueThreadPlanForStepOut
1432(
1433 bool abort_other_plans,
1434 SymbolContext *addr_context,
1435 bool first_insn,
1436 bool stop_other_threads,
1437 Vote stop_vote,
1438 Vote run_vote,
1439 uint32_t frame_idx
1440)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441{
Greg Clayton481cef22011-01-21 06:11:58 +00001442 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1443 addr_context,
1444 first_insn,
1445 stop_other_threads,
1446 stop_vote,
1447 run_vote,
1448 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001449
1450 if (thread_plan_sp->ValidatePlan(NULL))
1451 {
1452 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001453 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001454 }
1455 else
1456 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001457 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001458 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459}
1460
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001461ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001462Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001464 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001465 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001466 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001467
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001469 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470}
1471
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001472ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001473Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1474 Address& function,
1475 lldb::addr_t arg,
1476 bool stop_other_threads,
Jim Ingham184e9812013-01-15 02:47:48 +00001477 bool unwind_on_error,
1478 bool ignore_breakpoints)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479{
Jim Ingham184e9812013-01-15 02:47:48 +00001480 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1481 function,
1482 ClangASTType(),
1483 arg,
1484 stop_other_threads,
1485 unwind_on_error,
1486 ignore_breakpoints));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001488 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489}
1490
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001491ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1493 Address &target_addr,
1494 bool stop_other_threads)
1495{
1496 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1497 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001498 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499}
1500
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001501ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001503 lldb::addr_t *address_list,
1504 size_t num_addresses,
1505 bool stop_other_threads,
1506 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001507{
Greg Clayton481cef22011-01-21 06:11:58 +00001508 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001510 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511
1512}
1513
1514uint32_t
1515Thread::GetIndexID () const
1516{
1517 return m_index_id;
1518}
1519
1520void
1521Thread::DumpThreadPlans (lldb_private::Stream *s) const
1522{
1523 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001524 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001525 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001526 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 +00001527 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001530 s->Indent();
1531 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001532 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001534 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535 }
1536
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001537 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001538 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001539 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001540 s->Indent();
1541 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1542 for (i = stack_size - 1; i >= 0; i--)
1543 {
1544 s->IndentMore();
1545 s->Indent();
1546 s->Printf ("Element %d: ", i);
1547 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1548 s->EOL();
1549 s->IndentLess();
1550 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001551 }
1552
1553 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001554 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001555 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001556 s->Indent();
1557 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1558 for (i = stack_size - 1; i >= 0; i--)
1559 {
1560 s->IndentMore();
1561 s->Indent();
1562 s->Printf ("Element %d: ", i);
1563 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1564 s->EOL();
1565 s->IndentLess();
1566 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567 }
1568
1569}
1570
Greg Claytond9e416c2012-02-18 05:35:26 +00001571TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001572Thread::CalculateTarget ()
1573{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001574 TargetSP target_sp;
1575 ProcessSP process_sp(GetProcess());
1576 if (process_sp)
1577 target_sp = process_sp->CalculateTarget();
1578 return target_sp;
1579
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001580}
1581
Greg Claytond9e416c2012-02-18 05:35:26 +00001582ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583Thread::CalculateProcess ()
1584{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001585 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001586}
1587
Greg Claytond9e416c2012-02-18 05:35:26 +00001588ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589Thread::CalculateThread ()
1590{
Greg Claytond9e416c2012-02-18 05:35:26 +00001591 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592}
1593
Jason Molendab57e4a12013-11-04 09:33:30 +00001594StackFrameSP
1595Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001596{
Jason Molendab57e4a12013-11-04 09:33:30 +00001597 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001598}
1599
1600void
Greg Clayton0603aa92010-10-04 01:05:56 +00001601Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001602{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001603 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001604}
1605
Greg Clayton12daf9462010-08-25 00:35:26 +00001606
Greg Clayton39d0ab32012-03-29 01:41:38 +00001607StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001608Thread::GetStackFrameList ()
1609{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001610 StackFrameListSP frame_list_sp;
1611 Mutex::Locker locker(m_frame_mutex);
1612 if (m_curr_frames_sp)
1613 {
1614 frame_list_sp = m_curr_frames_sp;
1615 }
1616 else
1617 {
1618 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1619 m_curr_frames_sp = frame_list_sp;
1620 }
1621 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001622}
1623
Greg Clayton12daf9462010-08-25 00:35:26 +00001624void
1625Thread::ClearStackFrames ()
1626{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001627 Mutex::Locker locker(m_frame_mutex);
1628
Greg Clayton160c9d82013-05-01 21:54:04 +00001629 Unwind *unwinder = GetUnwinder ();
1630 if (unwinder)
1631 unwinder->Clear();
1632
Jim Inghamb0c72a52012-02-29 03:40:22 +00001633 // Only store away the old "reference" StackFrameList if we got all its frames:
1634 // FIXME: At some point we can try to splice in the frames we have fetched into
1635 // the new frame as we make it, but let's not try that now.
1636 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001637 m_prev_frames_sp.swap (m_curr_frames_sp);
1638 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001639}
1640
Jason Molendab57e4a12013-11-04 09:33:30 +00001641lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001642Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1643{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001644 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001645}
1646
Jim Ingham44137582012-09-12 00:40:39 +00001647
1648Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001649Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001650{
Jason Molendab57e4a12013-11-04 09:33:30 +00001651 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001652 Error return_error;
1653
1654 if (!frame_sp)
1655 {
Daniel Malead01b2952012-11-29 21:49:15 +00001656 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001657 }
1658
Jim Ingham4f465cf2012-10-10 18:32:14 +00001659 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001660}
1661
1662Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001663Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001664{
1665 Error return_error;
1666
1667 if (!frame_sp)
1668 {
1669 return_error.SetErrorString("Can't return to a null frame.");
1670 return return_error;
1671 }
1672
1673 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001674 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001675 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001676 if (!older_frame_sp)
1677 {
1678 return_error.SetErrorString("No older frame to return to.");
1679 return return_error;
1680 }
Jim Ingham44137582012-09-12 00:40:39 +00001681
1682 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001683 {
Jim Ingham44137582012-09-12 00:40:39 +00001684 lldb::ABISP abi = thread->GetProcess()->GetABI();
1685 if (!abi)
1686 {
1687 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001688 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001689 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001690 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1691
1692 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1693 // Turn that back on when that works.
1694 if (0 && sc.function != NULL)
1695 {
1696 Type *function_type = sc.function->GetType();
1697 if (function_type)
1698 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001699 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001700 if (return_type)
1701 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001702 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001703 return_type.DumpTypeDescription(&s);
1704 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001705 if (cast_value_sp)
1706 {
1707 cast_value_sp->SetFormat(eFormatHex);
1708 return_value_sp = cast_value_sp;
1709 }
1710 }
1711 }
1712 }
1713
Jim Inghamcb640dd2012-09-14 02:14:15 +00001714 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001715 if (!return_error.Success())
1716 return return_error;
1717 }
1718
1719 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001720 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001721 // cook their data
1722
Jason Molendab57e4a12013-11-04 09:33:30 +00001723 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001724 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001725 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001726 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1727 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001728 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001729 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1730 if (copy_success)
1731 {
1732 thread->DiscardThreadPlans(true);
1733 thread->ClearStackFrames();
1734 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1735 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1736 }
1737 else
1738 {
1739 return_error.SetErrorString("Could not reset register values.");
1740 }
Jim Ingham93208b82013-01-31 21:46:01 +00001741 }
1742 else
1743 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001744 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001745 }
Jim Ingham44137582012-09-12 00:40:39 +00001746 }
1747 else
1748 {
Jim Ingham93208b82013-01-31 21:46:01 +00001749 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001750 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001751 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001752}
1753
Richard Mittonf86248d2013-09-12 02:20:34 +00001754static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1755{
1756 for (size_t n=0;n<list.size();n++)
1757 {
1758 s << "\t";
1759 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1760 s << "\n";
1761 }
1762}
1763
1764Error
1765Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1766{
1767 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1768 Target *target = exe_ctx.GetTargetPtr();
1769 TargetSP target_sp = exe_ctx.GetTargetSP();
1770 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001771 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001772 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1773
1774 // Find candidate locations.
1775 std::vector<Address> candidates, within_function, outside_function;
1776 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1777
1778 // If possible, we try and stay within the current function.
1779 // Within a function, we accept multiple locations (optimized code may do this,
1780 // there's no solution here so we do the best we can).
1781 // However if we're trying to leave the function, we don't know how to pick the
1782 // right location, so if there's more than one then we bail.
1783 if (!within_function.empty())
1784 candidates = within_function;
1785 else if (outside_function.size() == 1 && can_leave_function)
1786 candidates = outside_function;
1787
1788 // Check if we got anything.
1789 if (candidates.empty())
1790 {
1791 if (outside_function.empty())
1792 {
1793 return Error("Cannot locate an address for %s:%i.",
1794 file.GetFilename().AsCString(), line);
1795 }
1796 else if (outside_function.size() == 1)
1797 {
1798 return Error("%s:%i is outside the current function.",
1799 file.GetFilename().AsCString(), line);
1800 }
1801 else
1802 {
1803 StreamString sstr;
1804 DumpAddressList(sstr, outside_function, target);
1805 return Error("%s:%i has multiple candidate locations:\n%s",
1806 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
1807 }
1808 }
1809
1810 // Accept the first location, warn about any others.
1811 Address dest = candidates[0];
1812 if (warnings && candidates.size() > 1)
1813 {
1814 StreamString sstr;
1815 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
1816 file.GetFilename().AsCString(), line);
1817 DumpAddressList(sstr, candidates, target);
1818 *warnings = sstr.GetString();
1819 }
1820
1821 if (!reg_ctx->SetPC (dest))
1822 return Error("Cannot change PC to target address.");
1823
1824 return Error();
1825}
1826
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827void
Greg Clayton0603aa92010-10-04 01:05:56 +00001828Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001829{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001830 ExecutionContext exe_ctx (shared_from_this());
1831 Process *process = exe_ctx.GetProcessPtr();
1832 if (process == NULL)
1833 return;
1834
Jason Molendab57e4a12013-11-04 09:33:30 +00001835 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001836 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001837 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001838 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001839 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001840 if (frame_sp)
1841 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001842 exe_ctx.SetFrameSP(frame_sp);
1843 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844 }
1845 }
1846
Greg Clayton1ac04c32012-02-21 00:09:25 +00001847 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001848 assert (thread_format);
Greg Clayton0603aa92010-10-04 01:05:56 +00001849 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001850 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001851 &exe_ctx,
1852 NULL,
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001853 strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001854}
1855
Greg Clayton99d0faf2010-11-18 23:32:35 +00001856void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001857Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001858{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001859}
Jim Inghamee8aea12010-09-08 03:14:33 +00001860
Greg Clayton99d0faf2010-11-18 23:32:35 +00001861void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001862Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001863{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001864}
Jim Inghamee8aea12010-09-08 03:14:33 +00001865
Richard Mitton0a558352013-10-17 21:14:00 +00001866lldb::addr_t
1867Thread::GetThreadPointer ()
1868{
1869 return LLDB_INVALID_ADDRESS;
1870}
1871
1872addr_t
1873Thread::GetThreadLocalData (const ModuleSP module)
1874{
1875 // The default implementation is to ask the dynamic loader for it.
1876 // This can be overridden for specific platforms.
1877 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
1878 if (loader)
1879 return loader->GetThreadLocalData (module, shared_from_this());
1880 else
1881 return LLDB_INVALID_ADDRESS;
1882}
1883
Jason Molendab57e4a12013-11-04 09:33:30 +00001884lldb::StackFrameSP
1885Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00001886{
Jason Molendab57e4a12013-11-04 09:33:30 +00001887 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001888}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001889
1890const char *
1891Thread::StopReasonAsCString (lldb::StopReason reason)
1892{
1893 switch (reason)
1894 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001895 case eStopReasonInvalid: return "invalid";
1896 case eStopReasonNone: return "none";
1897 case eStopReasonTrace: return "trace";
1898 case eStopReasonBreakpoint: return "breakpoint";
1899 case eStopReasonWatchpoint: return "watchpoint";
1900 case eStopReasonSignal: return "signal";
1901 case eStopReasonException: return "exception";
1902 case eStopReasonExec: return "exec";
1903 case eStopReasonPlanComplete: return "plan complete";
1904 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001905 }
1906
1907
1908 static char unknown_state_string[64];
1909 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1910 return unknown_state_string;
1911}
1912
1913const char *
1914Thread::RunModeAsCString (lldb::RunMode mode)
1915{
1916 switch (mode)
1917 {
1918 case eOnlyThisThread: return "only this thread";
1919 case eAllThreads: return "all threads";
1920 case eOnlyDuringStepping: return "only during stepping";
1921 }
1922
1923 static char unknown_state_string[64];
1924 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1925 return unknown_state_string;
1926}
Jim Ingham06e827c2010-11-11 19:26:09 +00001927
Greg Clayton7260f622011-04-18 08:33:37 +00001928size_t
1929Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1930{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001931 ExecutionContext exe_ctx (shared_from_this());
1932 Target *target = exe_ctx.GetTargetPtr();
1933 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001934 size_t num_frames_shown = 0;
1935 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001936 bool is_selected = false;
1937 if (process)
1938 {
1939 if (process->GetThreadList().GetSelectedThread().get() == this)
1940 is_selected = true;
1941 }
1942 strm.Printf("%c ", is_selected ? '*' : ' ');
1943 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001944 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001945 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001946 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001947 {
Jim Inghame610d642011-08-16 00:07:28 +00001948 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1949 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1950 {
1951 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1952 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001953 }
Greg Clayton7260f622011-04-18 08:33:37 +00001954 }
1955
1956 DumpUsingSettingsFormat (strm, start_frame);
1957
1958 if (num_frames > 0)
1959 {
1960 strm.IndentMore();
1961
1962 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001963
1964 const char *selected_frame_marker = NULL;
1965 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
1966 strm.IndentMore ();
1967 else
1968 selected_frame_marker = "* ";
1969
Greg Clayton39d0ab32012-03-29 01:41:38 +00001970 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1971 start_frame,
1972 num_frames,
1973 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001974 num_frames_with_source,
1975 selected_frame_marker);
1976 if (num_frames == 1)
1977 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001978 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001979 }
1980 return num_frames_shown;
1981}
1982
1983size_t
1984Thread::GetStackFrameStatus (Stream& strm,
1985 uint32_t first_frame,
1986 uint32_t num_frames,
1987 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001988 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001989{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001990 return GetStackFrameList()->GetStatus (strm,
1991 first_frame,
1992 num_frames,
1993 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001994 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001995}
1996
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001997bool
1998Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1999{
Jason Molendab57e4a12013-11-04 09:33:30 +00002000 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00002001 if (frame_sp)
2002 {
2003 checkpoint.SetStackID(frame_sp->GetStackID());
Greg Clayton1afa68e2013-04-02 20:32:37 +00002004 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
2005 if (reg_ctx_sp)
2006 return reg_ctx_sp->ReadAllRegisterValues (checkpoint.GetData());
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00002007 }
2008 return false;
2009}
Greg Clayton7260f622011-04-18 08:33:37 +00002010
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00002011bool
2012Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
2013{
Jim Ingham44137582012-09-12 00:40:39 +00002014 return ResetFrameZeroRegisters (checkpoint.GetData());
2015}
2016
2017bool
2018Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
2019{
Jason Molendab57e4a12013-11-04 09:33:30 +00002020 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00002021 if (frame_sp)
2022 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00002023 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
2024 if (reg_ctx_sp)
2025 {
2026 bool ret = reg_ctx_sp->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00002027
Greg Clayton1afa68e2013-04-02 20:32:37 +00002028 // Clear out all stack frames as our world just changed.
2029 ClearStackFrames();
2030 reg_ctx_sp->InvalidateIfNeeded(true);
2031 if (m_unwinder_ap.get())
2032 m_unwinder_ap->Clear();
2033 return ret;
2034 }
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00002035 }
2036 return false;
2037}
Greg Clayton7260f622011-04-18 08:33:37 +00002038
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002039Unwind *
2040Thread::GetUnwinder ()
2041{
2042 if (m_unwinder_ap.get() == NULL)
2043 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002044 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002045 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2046 switch (machine)
2047 {
2048 case llvm::Triple::x86_64:
2049 case llvm::Triple::x86:
2050 case llvm::Triple::arm:
2051 case llvm::Triple::thumb:
Ed Masteb73f8442013-10-10 00:59:47 +00002052 case llvm::Triple::mips64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002053 m_unwinder_ap.reset (new UnwindLLDB (*this));
2054 break;
2055
2056 default:
2057 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2058 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2059 break;
2060 }
2061 }
2062 return m_unwinder_ap.get();
2063}
2064
2065
Greg Claytonfa559e52012-05-18 02:38:05 +00002066void
2067Thread::Flush ()
2068{
2069 ClearStackFrames ();
2070 m_reg_context_sp.reset();
2071}
Jim Ingham5d88a062012-10-16 00:09:33 +00002072
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002073bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002074Thread::IsStillAtLastBreakpointHit ()
2075{
2076 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2077 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2078 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002079 if (m_stop_info_sp) {
2080 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002081 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002082 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002083 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2084 if (reg_ctx_sp)
2085 {
2086 lldb::addr_t pc = reg_ctx_sp->GetPC();
2087 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
2088 if (bp_site_sp && value == bp_site_sp->GetID())
2089 return true;
2090 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002091 }
2092 }
2093 return false;
2094}