blob: 07f5321990b26bc8e028346444267b581ec34164 [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{
Greg Claytonf74cf862013-11-13 23:28:31 +0000499 saved_state.register_backup_sp.reset();
500 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
501 if (frame_sp)
502 {
503 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
504 if (reg_checkpoint_sp)
505 {
506 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
507 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
508 saved_state.register_backup_sp = reg_checkpoint_sp;
509 }
510 }
511 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000512 return false;
513
514 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000515 ProcessSP process_sp (GetProcess());
516 if (process_sp)
517 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000518 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
519
Jim Ingham77787032011-01-20 02:03:18 +0000520 return true;
521}
522
523bool
Jim Ingham8559a352012-11-26 23:52:18 +0000524Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000525{
Greg Claytonf74cf862013-11-13 23:28:31 +0000526 if (saved_state.register_backup_sp)
527 {
528 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
529 if (frame_sp)
530 {
531 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
532 if (reg_ctx_sp)
533 {
534 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
535
536 // Clear out all stack frames as our world just changed.
537 ClearStackFrames();
538 reg_ctx_sp->InvalidateIfNeeded(true);
539 if (m_unwinder_ap.get())
540 m_unwinder_ap->Clear();
541 return ret;
542 }
543 }
544 }
545 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000546}
547
548bool
549Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
550{
Jim Ingham0c270682011-01-25 02:47:23 +0000551 if (saved_state.stop_info_sp)
552 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000553 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000554 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000555 return true;
556}
557
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558StateType
559Thread::GetState() const
560{
561 // If any other threads access this we will need a mutex for it
562 Mutex::Locker locker(m_state_mutex);
563 return m_state;
564}
565
566void
567Thread::SetState(StateType state)
568{
569 Mutex::Locker locker(m_state_mutex);
570 m_state = state;
571}
572
573void
574Thread::WillStop()
575{
576 ThreadPlan *current_plan = GetCurrentPlan();
577
578 // FIXME: I may decide to disallow threads with no plans. In which
579 // case this should go to an assert.
580
581 if (!current_plan)
582 return;
583
584 current_plan->WillStop();
585}
586
587void
588Thread::SetupForResume ()
589{
590 if (GetResumeState() != eStateSuspended)
591 {
592
593 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
594 // telling the current plan it will resume, since we might change what the current
595 // plan is.
596
Greg Clayton1afa68e2013-04-02 20:32:37 +0000597// StopReason stop_reason = lldb::eStopReasonInvalid;
598// StopInfoSP stop_info_sp = GetStopInfo();
599// if (stop_info_sp.get())
600// stop_reason = stop_info_sp->GetStopReason();
601// if (stop_reason == lldb::eStopReasonBreakpoint)
602 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
603 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000605 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
606 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000607 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000608 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
609 // special to step over a breakpoint.
610
611 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000612
Greg Clayton1afa68e2013-04-02 20:32:37 +0000613 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
614 {
615 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
616 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000618 ThreadPlanSP step_bp_plan_sp;
619 step_bp_plan->SetPrivate (true);
620
621 if (GetCurrentPlan()->RunState() != eStateStepping)
622 {
623 step_bp_plan->SetAutoContinue(true);
624 }
625 step_bp_plan_sp.reset (step_bp_plan);
626 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 }
629 }
630 }
631 }
632}
633
634bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000635Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636{
637 // At this point clear the completed plan stack.
638 m_completed_plan_stack.clear();
639 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000640 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641
Greg Clayton97d5cf02012-09-25 02:40:06 +0000642 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000643
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000644 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
645 if (backing_thread_sp)
646 backing_thread_sp->m_temporary_resume_state = resume_state;
647
648 // Make sure m_stop_info_sp is valid
649 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000650
Jim Ingham92087d82012-01-31 23:09:20 +0000651 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
652 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
653 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
654 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000655 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000656 if (m_stop_info_stop_id == process_stop_id &&
657 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000658 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000659 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000660 if (stop_info)
661 stop_info->WillResume (resume_state);
662 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663
664 // Tell all the plans that we are about to resume in case they need to clear any state.
665 // We distinguish between the plan on the top of the stack and the lower
666 // plans in case a plan needs to do any special business before it runs.
667
Greg Claytond1d06e42013-04-20 00:27:58 +0000668 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000670 if (plan_ptr)
671 {
672 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673
Greg Claytond1d06e42013-04-20 00:27:58 +0000674 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
675 {
676 plan_ptr->WillResume (resume_state, false);
677 }
678
679 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
680 // In that case, don't reset it here.
681
682 if (need_to_resume && resume_state != eStateSuspended)
683 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000684 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000685 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000686 }
687
Greg Clayton160c9d82013-05-01 21:54:04 +0000688 if (need_to_resume)
689 {
690 ClearStackFrames();
691 // Let Thread subclasses do any special work they need to prior to resuming
692 WillResume (resume_state);
693 }
694
Jim Ingham513c6bb2012-09-01 01:02:41 +0000695 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696}
697
698void
699Thread::DidResume ()
700{
701 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
702}
703
Andrew Kaylor29d65742013-05-10 17:19:04 +0000704void
705Thread::DidStop ()
706{
707 SetState (eStateStopped);
708}
709
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710bool
711Thread::ShouldStop (Event* event_ptr)
712{
713 ThreadPlan *current_plan = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000714
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 bool should_stop = true;
716
Greg Clayton5160ce52013-03-27 23:08:40 +0000717 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000718
719 if (GetResumeState () == eStateSuspended)
720 {
721 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000722 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 +0000723 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000724 GetID (),
725 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000726 return false;
727 }
728
729 if (GetTemporaryResumeState () == eStateSuspended)
730 {
731 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000732 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 +0000733 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000734 GetID (),
735 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000736 return false;
737 }
738
Daniel Malea246cb612013-05-14 15:20:12 +0000739 // Based on the current thread plan and process stop info, check if this
740 // thread caused the process to stop. NOTE: this must take place before
741 // the plan is moved from the current plan stack to the completed plan
742 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000743 if (ThreadStoppedForAReason() == false)
744 {
745 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000746 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 +0000747 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000748 GetID (),
749 GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000750 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000751 return false;
752 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000753
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 if (log)
755 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000756 log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
757 __FUNCTION__,
758 this,
Greg Clayton160c9d82013-05-01 21:54:04 +0000759 GetID (),
760 GetProtocolID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000761 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000762 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000764 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000766 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000768
769 // The top most plan always gets to do the trace log...
770 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000771
772 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
773 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
774 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000775 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000776 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
777 {
778 if (log)
779 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
780 return false;
781 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782
Jim Inghambad39e42012-09-05 21:12:49 +0000783 // If we've already been restarted, don't query the plans since the state they would examine is not current.
784 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
785 return false;
786
787 // Before the plans see the state of the world, calculate the current inlined depth.
788 GetStackFrameList()->CalculateCurrentInlinedDepth();
789
Jim Ingham25f66702011-12-03 01:52:59 +0000790 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
791 // If that plan is still working, then we don't need to do any more work. If the plan that explains
792 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
793 // whether they still need to do more work.
794
795 bool done_processing_current_plan = false;
796
Jim Ingham0161b492013-02-09 01:29:05 +0000797 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000798 {
799 if (current_plan->TracerExplainsStop())
800 {
801 done_processing_current_plan = true;
802 should_stop = false;
803 }
804 else
805 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000806 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000807 // does and let it handle the situation.
808 ThreadPlan *plan_ptr = current_plan;
809 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
810 {
Jim Ingham0161b492013-02-09 01:29:05 +0000811 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000812 {
813 should_stop = plan_ptr->ShouldStop (event_ptr);
814
815 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
816 // and all the plans below it off the stack.
817
818 if (plan_ptr->MischiefManaged())
819 {
Jim Ingham031177e2012-05-11 23:49:49 +0000820 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000821 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000822
823 do
824 {
825 if (should_stop)
826 current_plan->WillStop();
827 PopPlan();
828 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000829 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
830 // Now, if the responsible plan was not "Okay to discard" then we're done,
831 // otherwise we forward this to the next plan in the stack below.
832 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
833 done_processing_current_plan = true;
834 else
835 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000836 }
837 else
838 done_processing_current_plan = true;
839
840 break;
841 }
842
843 }
844 }
845 }
846
847 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000849 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000850
Jim Ingham10c4b242011-10-15 00:23:43 +0000851 if (log)
852 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
853
Jim Ingham0f16e732011-02-08 05:20:59 +0000854 // We're starting from the base plan, so just let it decide;
855 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000857 should_stop = current_plan->ShouldStop (event_ptr);
858 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000859 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000860 }
861 else
862 {
863 // Otherwise, don't let the base plan override what the other plans say to do, since
864 // presumably if there were other plans they would know what to do...
865 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000867 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000869
870 should_stop = current_plan->ShouldStop(event_ptr);
871 if (log)
872 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
873 if (current_plan->MischiefManaged())
874 {
875 if (should_stop)
876 current_plan->WillStop();
877
878 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
879 // Otherwise, see if the plan's parent wants to stop.
880
881 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
882 {
883 PopPlan();
884 break;
885 }
886 else
887 {
888
889 PopPlan();
890
891 current_plan = GetCurrentPlan();
892 if (current_plan == NULL)
893 {
894 break;
895 }
896 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 }
898 else
899 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000900 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 }
903 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000904
Jim Inghamb01e7422010-06-19 04:45:32 +0000905 if (over_ride_stop)
906 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000907
908 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
909 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
910 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
911 // This code clears stale plans off the stack.
912
913 if (should_stop)
914 {
915 ThreadPlan *plan_ptr = GetCurrentPlan();
916 while (!PlanIsBasePlan(plan_ptr))
917 {
918 bool stale = plan_ptr->IsPlanStale ();
919 ThreadPlan *examined_plan = plan_ptr;
920 plan_ptr = GetPreviousPlan (examined_plan);
921
922 if (stale)
923 {
924 if (log)
925 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
926 DiscardThreadPlansUpToPlan(examined_plan);
927 }
928 }
929 }
930
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932
Jim Ingham10c4b242011-10-15 00:23:43 +0000933 if (log)
934 {
935 StreamString s;
936 s.IndentMore();
937 DumpThreadPlans(&s);
938 log->Printf ("Plan stack final state:\n%s", s.GetData());
939 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
940 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 return should_stop;
942}
943
944Vote
945Thread::ShouldReportStop (Event* event_ptr)
946{
947 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000948 StateType temp_thread_state = GetTemporaryResumeState();
949
Greg Clayton5160ce52013-03-27 23:08:40 +0000950 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000951
952 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
953 {
954 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000955 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 +0000956 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000957 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958
Jim Ingham92087d82012-01-31 23:09:20 +0000959 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
960 {
961 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000962 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 +0000963 return eVoteNoOpinion;
964 }
965
966 if (!ThreadStoppedForAReason())
967 {
968 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000969 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 +0000970 return eVoteNoOpinion;
971 }
972
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 if (m_completed_plan_stack.size() > 0)
974 {
975 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000976 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000977 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 +0000978 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
979 }
980 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000981 {
Jim Ingham0161b492013-02-09 01:29:05 +0000982 Vote thread_vote = eVoteNoOpinion;
983 ThreadPlan *plan_ptr = GetCurrentPlan();
984 while (1)
985 {
986 if (plan_ptr->PlanExplainsStop(event_ptr))
987 {
988 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
989 break;
990 }
991 if (PlanIsBasePlan(plan_ptr))
992 break;
993 else
994 plan_ptr = GetPreviousPlan(plan_ptr);
995 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000996 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000997 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 +0000998
999 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001000 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001}
1002
1003Vote
1004Thread::ShouldReportRun (Event* event_ptr)
1005{
1006 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +00001007
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008 if (thread_state == eStateSuspended
1009 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001010 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001012 }
1013
Greg Clayton5160ce52013-03-27 23:08:40 +00001014 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 if (m_completed_plan_stack.size() > 0)
1016 {
1017 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001018 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001019 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
1020 GetIndexID(),
1021 this,
Jim Ingham444586b2011-01-24 06:34:17 +00001022 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001023 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001024 m_completed_plan_stack.back()->GetName());
1025
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1027 }
1028 else
Jim Ingham444586b2011-01-24 06:34:17 +00001029 {
1030 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001031 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
1032 GetIndexID(),
1033 this,
Jim Ingham444586b2011-01-24 06:34:17 +00001034 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001035 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001036 GetCurrentPlan()->GetName());
1037
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001039 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040}
1041
Jim Ingham1b54c882010-06-16 02:00:15 +00001042bool
1043Thread::MatchesSpec (const ThreadSpec *spec)
1044{
1045 if (spec == NULL)
1046 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001047
Jim Ingham3d902922012-03-07 22:03:04 +00001048 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001049}
1050
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051void
1052Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1053{
1054 if (thread_plan_sp)
1055 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001056 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1057 if (!thread_plan_sp->GetThreadPlanTracer())
1058 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001059 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +00001060
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061 thread_plan_sp->DidPush();
1062
Greg Clayton5160ce52013-03-27 23:08:40 +00001063 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064 if (log)
1065 {
1066 StreamString s;
1067 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001068 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1069 this,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001071 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 }
1073 }
1074}
1075
1076void
1077Thread::PopPlan ()
1078{
Greg Clayton5160ce52013-03-27 23:08:40 +00001079 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001081 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082 return;
1083 else
1084 {
1085 ThreadPlanSP &plan = m_plan_stack.back();
1086 if (log)
1087 {
Daniel Malead01b2952012-11-29 21:49:15 +00001088 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 }
1090 m_completed_plan_stack.push_back (plan);
1091 plan->WillPop();
1092 m_plan_stack.pop_back();
1093 }
1094}
1095
1096void
1097Thread::DiscardPlan ()
1098{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001099 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 if (m_plan_stack.size() > 1)
1101 {
1102 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001103 if (log)
1104 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1105
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 m_discarded_plan_stack.push_back (plan);
1107 plan->WillPop();
1108 m_plan_stack.pop_back();
1109 }
1110}
1111
1112ThreadPlan *
1113Thread::GetCurrentPlan ()
1114{
Jim Inghame1471232012-04-19 00:17:05 +00001115 // There will always be at least the base plan. If somebody is mucking with a
1116 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001117 if (m_plan_stack.empty())
1118 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001119 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120}
1121
1122ThreadPlanSP
1123Thread::GetCompletedPlan ()
1124{
1125 ThreadPlanSP empty_plan_sp;
1126 if (!m_completed_plan_stack.empty())
1127 {
1128 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1129 {
1130 ThreadPlanSP completed_plan_sp;
1131 completed_plan_sp = m_completed_plan_stack[i];
1132 if (!completed_plan_sp->GetPrivate ())
1133 return completed_plan_sp;
1134 }
1135 }
1136 return empty_plan_sp;
1137}
1138
Jim Ingham73ca05a2011-12-17 01:35:57 +00001139ValueObjectSP
1140Thread::GetReturnValueObject ()
1141{
1142 if (!m_completed_plan_stack.empty())
1143 {
1144 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1145 {
1146 ValueObjectSP return_valobj_sp;
1147 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1148 if (return_valobj_sp)
1149 return return_valobj_sp;
1150 }
1151 }
1152 return ValueObjectSP();
1153}
1154
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155bool
1156Thread::IsThreadPlanDone (ThreadPlan *plan)
1157{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 if (!m_completed_plan_stack.empty())
1159 {
1160 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1161 {
1162 if (m_completed_plan_stack[i].get() == plan)
1163 return true;
1164 }
1165 }
1166 return false;
1167}
1168
1169bool
1170Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1171{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172 if (!m_discarded_plan_stack.empty())
1173 {
1174 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1175 {
1176 if (m_discarded_plan_stack[i].get() == plan)
1177 return true;
1178 }
1179 }
1180 return false;
1181}
1182
1183ThreadPlan *
1184Thread::GetPreviousPlan (ThreadPlan *current_plan)
1185{
1186 if (current_plan == NULL)
1187 return NULL;
1188
1189 int stack_size = m_completed_plan_stack.size();
1190 for (int i = stack_size - 1; i > 0; i--)
1191 {
1192 if (current_plan == m_completed_plan_stack[i].get())
1193 return m_completed_plan_stack[i-1].get();
1194 }
1195
1196 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1197 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 if (m_plan_stack.size() > 0)
1199 return m_plan_stack.back().get();
1200 else
1201 return NULL;
1202 }
1203
1204 stack_size = m_plan_stack.size();
1205 for (int i = stack_size - 1; i > 0; i--)
1206 {
1207 if (current_plan == m_plan_stack[i].get())
1208 return m_plan_stack[i-1].get();
1209 }
1210 return NULL;
1211}
1212
1213void
1214Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1215{
1216 if (abort_other_plans)
1217 DiscardThreadPlans(true);
1218
1219 PushPlan (thread_plan_sp);
1220}
1221
Jim Ingham06e827c2010-11-11 19:26:09 +00001222
1223void
1224Thread::EnableTracer (bool value, bool single_stepping)
1225{
1226 int stack_size = m_plan_stack.size();
1227 for (int i = 0; i < stack_size; i++)
1228 {
1229 if (m_plan_stack[i]->GetThreadPlanTracer())
1230 {
1231 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1232 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1233 }
1234 }
1235}
1236
1237void
1238Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1239{
1240 int stack_size = m_plan_stack.size();
1241 for (int i = 0; i < stack_size; i++)
1242 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1243}
1244
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001246Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1247{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001248 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1249}
1250
1251void
1252Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1253{
Greg Clayton5160ce52013-03-27 23:08:40 +00001254 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001255 if (log)
1256 {
Daniel Malead01b2952012-11-29 21:49:15 +00001257 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 +00001258 }
1259
1260 int stack_size = m_plan_stack.size();
1261
1262 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1263 // stack, and if so discard up to and including it.
1264
Jim Ingham64e7ead2012-05-03 21:19:36 +00001265 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001266 {
1267 for (int i = stack_size - 1; i > 0; i--)
1268 DiscardPlan();
1269 }
1270 else
1271 {
1272 bool found_it = false;
1273 for (int i = stack_size - 1; i > 0; i--)
1274 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001275 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001276 found_it = true;
1277 }
1278 if (found_it)
1279 {
1280 bool last_one = false;
1281 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1282 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001283 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001284 last_one = true;
1285 DiscardPlan();
1286 }
1287 }
1288 }
1289 return;
1290}
1291
1292void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293Thread::DiscardThreadPlans(bool force)
1294{
Greg Clayton5160ce52013-03-27 23:08:40 +00001295 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296 if (log)
1297 {
Daniel Malead01b2952012-11-29 21:49:15 +00001298 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299 }
1300
1301 if (force)
1302 {
1303 int stack_size = m_plan_stack.size();
1304 for (int i = stack_size - 1; i > 0; i--)
1305 {
1306 DiscardPlan();
1307 }
1308 return;
1309 }
1310
1311 while (1)
1312 {
1313
1314 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001315 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316
1317 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1318 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1319 {
1320 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1321 {
1322 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1323 break;
1324 }
1325 }
1326
1327 if (discard)
1328 {
1329 // First pop all the dependent plans:
1330 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1331 {
1332
1333 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1334 // for the plan leaves it in a state that it is safe to pop the plan
1335 // with no more notice?
1336 DiscardPlan();
1337 }
1338
1339 // Now discard the master plan itself.
1340 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1341 // discard it's dependent plans, but not it...
1342 if (master_plan_idx > 0)
1343 {
1344 DiscardPlan();
1345 }
1346 }
1347 else
1348 {
1349 // If the master plan doesn't want to get discarded, then we're done.
1350 break;
1351 }
1352
1353 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354}
1355
Jim Inghamcf274f92012-04-09 22:37:39 +00001356bool
1357Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1358{
1359 if (plan_ptr->IsBasePlan())
1360 return true;
1361 else if (m_plan_stack.size() == 0)
1362 return false;
1363 else
1364 return m_plan_stack[0].get() == plan_ptr;
1365}
1366
Jim Ingham93208b82013-01-31 21:46:01 +00001367Error
1368Thread::UnwindInnermostExpression()
1369{
1370 Error error;
1371 int stack_size = m_plan_stack.size();
1372
1373 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1374 // stack, and if so discard up to and including it.
1375
1376 for (int i = stack_size - 1; i > 0; i--)
1377 {
1378 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1379 {
1380 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1381 return error;
1382 }
1383 }
1384 error.SetErrorString("No expressions currently active on this thread");
1385 return error;
1386}
1387
1388
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001389ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390Thread::QueueFundamentalPlan (bool abort_other_plans)
1391{
1392 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1393 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001394 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395}
1396
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001397ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001398Thread::QueueThreadPlanForStepSingleInstruction
1399(
1400 bool step_over,
1401 bool abort_other_plans,
1402 bool stop_other_threads
1403)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404{
1405 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1406 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001407 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408}
1409
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001410ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001411Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001412(
1413 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001414 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001415 const SymbolContext &addr_context,
1416 lldb::RunMode stop_other_threads
1417)
1418{
1419 ThreadPlanSP thread_plan_sp;
1420 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1421
1422 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001423 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001424}
1425
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001426ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001427Thread::QueueThreadPlanForStepInRange
1428(
1429 bool abort_other_plans,
1430 const AddressRange &range,
1431 const SymbolContext &addr_context,
1432 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001433 lldb::RunMode stop_other_threads,
1434 bool avoid_code_without_debug_info
1435)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436{
1437 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001438 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1439 if (avoid_code_without_debug_info)
1440 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441 else
Jim Inghamc6276822012-12-12 19:58:40 +00001442 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1443 if (step_in_target)
1444 plan->SetStepInTarget(step_in_target);
1445 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446
1447 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001448 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449}
1450
1451
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001452ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001453Thread::QueueThreadPlanForStepOut
1454(
1455 bool abort_other_plans,
1456 SymbolContext *addr_context,
1457 bool first_insn,
1458 bool stop_other_threads,
1459 Vote stop_vote,
1460 Vote run_vote,
1461 uint32_t frame_idx
1462)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463{
Greg Clayton481cef22011-01-21 06:11:58 +00001464 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1465 addr_context,
1466 first_insn,
1467 stop_other_threads,
1468 stop_vote,
1469 run_vote,
1470 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001471
1472 if (thread_plan_sp->ValidatePlan(NULL))
1473 {
1474 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001475 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001476 }
1477 else
1478 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001479 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001480 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481}
1482
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001483ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001484Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001486 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001487 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001488 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001489
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001491 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492}
1493
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001494ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1496 Address &target_addr,
1497 bool stop_other_threads)
1498{
1499 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1500 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001501 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502}
1503
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001504ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001505Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001506 lldb::addr_t *address_list,
1507 size_t num_addresses,
1508 bool stop_other_threads,
1509 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510{
Greg Clayton481cef22011-01-21 06:11:58 +00001511 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001513 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514
1515}
1516
1517uint32_t
1518Thread::GetIndexID () const
1519{
1520 return m_index_id;
1521}
1522
1523void
1524Thread::DumpThreadPlans (lldb_private::Stream *s) const
1525{
1526 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001527 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001528 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001529 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 +00001530 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001532 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001533 s->Indent();
1534 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001536 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001537 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538 }
1539
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001540 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001541 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001542 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001543 s->Indent();
1544 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1545 for (i = stack_size - 1; i >= 0; i--)
1546 {
1547 s->IndentMore();
1548 s->Indent();
1549 s->Printf ("Element %d: ", i);
1550 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1551 s->EOL();
1552 s->IndentLess();
1553 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554 }
1555
1556 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001557 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001558 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001559 s->Indent();
1560 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1561 for (i = stack_size - 1; i >= 0; i--)
1562 {
1563 s->IndentMore();
1564 s->Indent();
1565 s->Printf ("Element %d: ", i);
1566 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1567 s->EOL();
1568 s->IndentLess();
1569 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570 }
1571
1572}
1573
Greg Claytond9e416c2012-02-18 05:35:26 +00001574TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575Thread::CalculateTarget ()
1576{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001577 TargetSP target_sp;
1578 ProcessSP process_sp(GetProcess());
1579 if (process_sp)
1580 target_sp = process_sp->CalculateTarget();
1581 return target_sp;
1582
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583}
1584
Greg Claytond9e416c2012-02-18 05:35:26 +00001585ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001586Thread::CalculateProcess ()
1587{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001588 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589}
1590
Greg Claytond9e416c2012-02-18 05:35:26 +00001591ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592Thread::CalculateThread ()
1593{
Greg Claytond9e416c2012-02-18 05:35:26 +00001594 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001595}
1596
Jason Molendab57e4a12013-11-04 09:33:30 +00001597StackFrameSP
1598Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001599{
Jason Molendab57e4a12013-11-04 09:33:30 +00001600 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001601}
1602
1603void
Greg Clayton0603aa92010-10-04 01:05:56 +00001604Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001605{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001606 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001607}
1608
Greg Clayton12daf9462010-08-25 00:35:26 +00001609
Greg Clayton39d0ab32012-03-29 01:41:38 +00001610StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001611Thread::GetStackFrameList ()
1612{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001613 StackFrameListSP frame_list_sp;
1614 Mutex::Locker locker(m_frame_mutex);
1615 if (m_curr_frames_sp)
1616 {
1617 frame_list_sp = m_curr_frames_sp;
1618 }
1619 else
1620 {
1621 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1622 m_curr_frames_sp = frame_list_sp;
1623 }
1624 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001625}
1626
Greg Clayton12daf9462010-08-25 00:35:26 +00001627void
1628Thread::ClearStackFrames ()
1629{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001630 Mutex::Locker locker(m_frame_mutex);
1631
Greg Clayton160c9d82013-05-01 21:54:04 +00001632 Unwind *unwinder = GetUnwinder ();
1633 if (unwinder)
1634 unwinder->Clear();
1635
Jim Inghamb0c72a52012-02-29 03:40:22 +00001636 // Only store away the old "reference" StackFrameList if we got all its frames:
1637 // FIXME: At some point we can try to splice in the frames we have fetched into
1638 // the new frame as we make it, but let's not try that now.
1639 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001640 m_prev_frames_sp.swap (m_curr_frames_sp);
1641 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001642}
1643
Jason Molendab57e4a12013-11-04 09:33:30 +00001644lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001645Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1646{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001647 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001648}
1649
Jim Ingham44137582012-09-12 00:40:39 +00001650
1651Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001652Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001653{
Jason Molendab57e4a12013-11-04 09:33:30 +00001654 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001655 Error return_error;
1656
1657 if (!frame_sp)
1658 {
Daniel Malead01b2952012-11-29 21:49:15 +00001659 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001660 }
1661
Jim Ingham4f465cf2012-10-10 18:32:14 +00001662 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001663}
1664
1665Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001666Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001667{
1668 Error return_error;
1669
1670 if (!frame_sp)
1671 {
1672 return_error.SetErrorString("Can't return to a null frame.");
1673 return return_error;
1674 }
1675
1676 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001677 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001678 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001679 if (!older_frame_sp)
1680 {
1681 return_error.SetErrorString("No older frame to return to.");
1682 return return_error;
1683 }
Jim Ingham44137582012-09-12 00:40:39 +00001684
1685 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001686 {
Jim Ingham44137582012-09-12 00:40:39 +00001687 lldb::ABISP abi = thread->GetProcess()->GetABI();
1688 if (!abi)
1689 {
1690 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001691 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001692 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001693 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1694
1695 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1696 // Turn that back on when that works.
1697 if (0 && sc.function != NULL)
1698 {
1699 Type *function_type = sc.function->GetType();
1700 if (function_type)
1701 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001702 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001703 if (return_type)
1704 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001705 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001706 return_type.DumpTypeDescription(&s);
1707 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001708 if (cast_value_sp)
1709 {
1710 cast_value_sp->SetFormat(eFormatHex);
1711 return_value_sp = cast_value_sp;
1712 }
1713 }
1714 }
1715 }
1716
Jim Inghamcb640dd2012-09-14 02:14:15 +00001717 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001718 if (!return_error.Success())
1719 return return_error;
1720 }
1721
1722 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001723 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001724 // cook their data
1725
Jason Molendab57e4a12013-11-04 09:33:30 +00001726 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001727 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001728 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001729 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1730 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001731 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001732 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1733 if (copy_success)
1734 {
1735 thread->DiscardThreadPlans(true);
1736 thread->ClearStackFrames();
1737 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1738 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1739 }
1740 else
1741 {
1742 return_error.SetErrorString("Could not reset register values.");
1743 }
Jim Ingham93208b82013-01-31 21:46:01 +00001744 }
1745 else
1746 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001747 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001748 }
Jim Ingham44137582012-09-12 00:40:39 +00001749 }
1750 else
1751 {
Jim Ingham93208b82013-01-31 21:46:01 +00001752 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001753 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001754 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001755}
1756
Richard Mittonf86248d2013-09-12 02:20:34 +00001757static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1758{
1759 for (size_t n=0;n<list.size();n++)
1760 {
1761 s << "\t";
1762 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1763 s << "\n";
1764 }
1765}
1766
1767Error
1768Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1769{
1770 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1771 Target *target = exe_ctx.GetTargetPtr();
1772 TargetSP target_sp = exe_ctx.GetTargetSP();
1773 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001774 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001775 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1776
1777 // Find candidate locations.
1778 std::vector<Address> candidates, within_function, outside_function;
1779 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1780
1781 // If possible, we try and stay within the current function.
1782 // Within a function, we accept multiple locations (optimized code may do this,
1783 // there's no solution here so we do the best we can).
1784 // However if we're trying to leave the function, we don't know how to pick the
1785 // right location, so if there's more than one then we bail.
1786 if (!within_function.empty())
1787 candidates = within_function;
1788 else if (outside_function.size() == 1 && can_leave_function)
1789 candidates = outside_function;
1790
1791 // Check if we got anything.
1792 if (candidates.empty())
1793 {
1794 if (outside_function.empty())
1795 {
1796 return Error("Cannot locate an address for %s:%i.",
1797 file.GetFilename().AsCString(), line);
1798 }
1799 else if (outside_function.size() == 1)
1800 {
1801 return Error("%s:%i is outside the current function.",
1802 file.GetFilename().AsCString(), line);
1803 }
1804 else
1805 {
1806 StreamString sstr;
1807 DumpAddressList(sstr, outside_function, target);
1808 return Error("%s:%i has multiple candidate locations:\n%s",
1809 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
1810 }
1811 }
1812
1813 // Accept the first location, warn about any others.
1814 Address dest = candidates[0];
1815 if (warnings && candidates.size() > 1)
1816 {
1817 StreamString sstr;
1818 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
1819 file.GetFilename().AsCString(), line);
1820 DumpAddressList(sstr, candidates, target);
1821 *warnings = sstr.GetString();
1822 }
1823
1824 if (!reg_ctx->SetPC (dest))
1825 return Error("Cannot change PC to target address.");
1826
1827 return Error();
1828}
1829
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001830void
Greg Clayton0603aa92010-10-04 01:05:56 +00001831Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001832{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001833 ExecutionContext exe_ctx (shared_from_this());
1834 Process *process = exe_ctx.GetProcessPtr();
1835 if (process == NULL)
1836 return;
1837
Jason Molendab57e4a12013-11-04 09:33:30 +00001838 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001839 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001840 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001842 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001843 if (frame_sp)
1844 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001845 exe_ctx.SetFrameSP(frame_sp);
1846 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847 }
1848 }
1849
Greg Clayton1ac04c32012-02-21 00:09:25 +00001850 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001851 assert (thread_format);
Greg Clayton0603aa92010-10-04 01:05:56 +00001852 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001853 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001854 &exe_ctx,
1855 NULL,
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001856 strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001857}
1858
Greg Clayton99d0faf2010-11-18 23:32:35 +00001859void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001860Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001861{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001862}
Jim Inghamee8aea12010-09-08 03:14:33 +00001863
Greg Clayton99d0faf2010-11-18 23:32:35 +00001864void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001865Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001866{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001867}
Jim Inghamee8aea12010-09-08 03:14:33 +00001868
Richard Mitton0a558352013-10-17 21:14:00 +00001869lldb::addr_t
1870Thread::GetThreadPointer ()
1871{
1872 return LLDB_INVALID_ADDRESS;
1873}
1874
1875addr_t
1876Thread::GetThreadLocalData (const ModuleSP module)
1877{
1878 // The default implementation is to ask the dynamic loader for it.
1879 // This can be overridden for specific platforms.
1880 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
1881 if (loader)
1882 return loader->GetThreadLocalData (module, shared_from_this());
1883 else
1884 return LLDB_INVALID_ADDRESS;
1885}
1886
Jason Molendab57e4a12013-11-04 09:33:30 +00001887lldb::StackFrameSP
1888Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00001889{
Jason Molendab57e4a12013-11-04 09:33:30 +00001890 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001891}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001892
1893const char *
1894Thread::StopReasonAsCString (lldb::StopReason reason)
1895{
1896 switch (reason)
1897 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001898 case eStopReasonInvalid: return "invalid";
1899 case eStopReasonNone: return "none";
1900 case eStopReasonTrace: return "trace";
1901 case eStopReasonBreakpoint: return "breakpoint";
1902 case eStopReasonWatchpoint: return "watchpoint";
1903 case eStopReasonSignal: return "signal";
1904 case eStopReasonException: return "exception";
1905 case eStopReasonExec: return "exec";
1906 case eStopReasonPlanComplete: return "plan complete";
1907 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001908 }
1909
1910
1911 static char unknown_state_string[64];
1912 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1913 return unknown_state_string;
1914}
1915
1916const char *
1917Thread::RunModeAsCString (lldb::RunMode mode)
1918{
1919 switch (mode)
1920 {
1921 case eOnlyThisThread: return "only this thread";
1922 case eAllThreads: return "all threads";
1923 case eOnlyDuringStepping: return "only during stepping";
1924 }
1925
1926 static char unknown_state_string[64];
1927 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1928 return unknown_state_string;
1929}
Jim Ingham06e827c2010-11-11 19:26:09 +00001930
Greg Clayton7260f622011-04-18 08:33:37 +00001931size_t
1932Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1933{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001934 ExecutionContext exe_ctx (shared_from_this());
1935 Target *target = exe_ctx.GetTargetPtr();
1936 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001937 size_t num_frames_shown = 0;
1938 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001939 bool is_selected = false;
1940 if (process)
1941 {
1942 if (process->GetThreadList().GetSelectedThread().get() == this)
1943 is_selected = true;
1944 }
1945 strm.Printf("%c ", is_selected ? '*' : ' ');
1946 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001947 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001948 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001949 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001950 {
Jim Inghame610d642011-08-16 00:07:28 +00001951 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1952 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1953 {
1954 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1955 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001956 }
Greg Clayton7260f622011-04-18 08:33:37 +00001957 }
1958
1959 DumpUsingSettingsFormat (strm, start_frame);
1960
1961 if (num_frames > 0)
1962 {
1963 strm.IndentMore();
1964
1965 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001966
1967 const char *selected_frame_marker = NULL;
1968 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
1969 strm.IndentMore ();
1970 else
1971 selected_frame_marker = "* ";
1972
Greg Clayton39d0ab32012-03-29 01:41:38 +00001973 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1974 start_frame,
1975 num_frames,
1976 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001977 num_frames_with_source,
1978 selected_frame_marker);
1979 if (num_frames == 1)
1980 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001981 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001982 }
1983 return num_frames_shown;
1984}
1985
1986size_t
1987Thread::GetStackFrameStatus (Stream& strm,
1988 uint32_t first_frame,
1989 uint32_t num_frames,
1990 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001991 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001992{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001993 return GetStackFrameList()->GetStatus (strm,
1994 first_frame,
1995 num_frames,
1996 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001997 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001998}
1999
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002000Unwind *
2001Thread::GetUnwinder ()
2002{
2003 if (m_unwinder_ap.get() == NULL)
2004 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002005 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002006 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2007 switch (machine)
2008 {
2009 case llvm::Triple::x86_64:
2010 case llvm::Triple::x86:
2011 case llvm::Triple::arm:
2012 case llvm::Triple::thumb:
Ed Masteb73f8442013-10-10 00:59:47 +00002013 case llvm::Triple::mips64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002014 m_unwinder_ap.reset (new UnwindLLDB (*this));
2015 break;
2016
2017 default:
2018 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2019 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2020 break;
2021 }
2022 }
2023 return m_unwinder_ap.get();
2024}
2025
2026
Greg Claytonfa559e52012-05-18 02:38:05 +00002027void
2028Thread::Flush ()
2029{
2030 ClearStackFrames ();
2031 m_reg_context_sp.reset();
2032}
Jim Ingham5d88a062012-10-16 00:09:33 +00002033
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002034bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002035Thread::IsStillAtLastBreakpointHit ()
2036{
2037 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2038 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2039 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002040 if (m_stop_info_sp) {
2041 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002042 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002043 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002044 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2045 if (reg_ctx_sp)
2046 {
2047 lldb::addr_t pc = reg_ctx_sp->GetPC();
2048 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
2049 if (bp_site_sp && value == bp_site_sp->GetID())
2050 return true;
2051 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002052 }
2053 }
2054 return false;
2055}