blob: 646a63511e933a6de15af96f89514a5ec01d0ec7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/lldb-private-log.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000013#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000014#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000016#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000019#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000020#include "lldb/Host/Host.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000021#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Target/DynamicLoader.h"
23#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000024#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000027#include "lldb/Target/StopInfo.h"
Greg Clayton896dff62010-07-23 16:45:51 +000028#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlan.h"
31#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/ThreadPlanBase.h"
33#include "lldb/Target/ThreadPlanStepInstruction.h"
34#include "lldb/Target/ThreadPlanStepOut.h"
35#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
36#include "lldb/Target/ThreadPlanStepThrough.h"
37#include "lldb/Target/ThreadPlanStepInRange.h"
38#include "lldb/Target/ThreadPlanStepOverRange.h"
39#include "lldb/Target/ThreadPlanRunToAddress.h"
40#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000041#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000042#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000043#include "Plugins/Process/Utility/UnwindLLDB.h"
44#include "UnwindMacOSXFrameBackchain.h"
45
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb;
48using namespace lldb_private;
49
Greg Clayton67cc0632012-08-22 17:17:09 +000050
51const ThreadPropertiesSP &
52Thread::GetGlobalProperties()
53{
54 static ThreadPropertiesSP g_settings_sp;
55 if (!g_settings_sp)
56 g_settings_sp.reset (new ThreadProperties (true));
57 return g_settings_sp;
58}
59
60static PropertyDefinition
61g_properties[] =
62{
63 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
64 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
65 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
66};
67
68enum {
69 ePropertyStepAvoidRegex,
70 ePropertyEnableThreadTrace
71};
72
73
74class ThreadOptionValueProperties : public OptionValueProperties
75{
76public:
77 ThreadOptionValueProperties (const ConstString &name) :
78 OptionValueProperties (name)
79 {
80 }
81
82 // This constructor is used when creating ThreadOptionValueProperties when it
83 // is part of a new lldb_private::Thread instance. It will copy all current
84 // global property values as needed
85 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000086 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000087 {
88 }
89
90 virtual const Property *
91 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
92 {
93 // When gettings the value for a key from the thread options, we will always
94 // try and grab the setting from the current thread if there is one. Else we just
95 // use the one from this instance.
96 if (exe_ctx)
97 {
98 Thread *thread = exe_ctx->GetThreadPtr();
99 if (thread)
100 {
101 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
102 if (this != instance_properties)
103 return instance_properties->ProtectedGetPropertyAtIndex (idx);
104 }
105 }
106 return ProtectedGetPropertyAtIndex (idx);
107 }
108};
109
110
111
112ThreadProperties::ThreadProperties (bool is_global) :
113 Properties ()
114{
115 if (is_global)
116 {
117 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
118 m_collection_sp->Initialize(g_properties);
119 }
120 else
121 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
122}
123
124ThreadProperties::~ThreadProperties()
125{
126}
127
128const RegularExpression *
129ThreadProperties::GetSymbolsToAvoidRegexp()
130{
131 const uint32_t idx = ePropertyStepAvoidRegex;
132 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
133}
134
135bool
136ThreadProperties::GetTraceEnabledState() const
137{
138 const uint32_t idx = ePropertyEnableThreadTrace;
139 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
140}
141
Jim Ingham4f465cf2012-10-10 18:32:14 +0000142//------------------------------------------------------------------
143// Thread Event Data
144//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000145
Jim Ingham4f465cf2012-10-10 18:32:14 +0000146
147const ConstString &
148Thread::ThreadEventData::GetFlavorString ()
149{
150 static ConstString g_flavor ("Thread::ThreadEventData");
151 return g_flavor;
152}
153
154Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
155 m_thread_sp (thread_sp),
156 m_stack_id ()
157{
158}
159
160Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
161 m_thread_sp (thread_sp),
162 m_stack_id (stack_id)
163{
164}
165
166Thread::ThreadEventData::ThreadEventData () :
167 m_thread_sp (),
168 m_stack_id ()
169{
170}
171
172Thread::ThreadEventData::~ThreadEventData ()
173{
174}
175
176void
177Thread::ThreadEventData::Dump (Stream *s) const
178{
179
180}
181
182const Thread::ThreadEventData *
183Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
184{
185 if (event_ptr)
186 {
187 const EventData *event_data = event_ptr->GetData();
188 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
189 return static_cast <const ThreadEventData *> (event_ptr->GetData());
190 }
191 return NULL;
192}
193
194ThreadSP
195Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
196{
197 ThreadSP thread_sp;
198 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
199 if (event_data)
200 thread_sp = event_data->GetThread();
201 return thread_sp;
202}
203
204StackID
205Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
206{
207 StackID stack_id;
208 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
209 if (event_data)
210 stack_id = event_data->GetStackID();
211 return stack_id;
212}
213
214StackFrameSP
215Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
216{
217 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
218 StackFrameSP frame_sp;
219 if (event_data)
220 {
221 ThreadSP thread_sp = event_data->GetThread();
222 if (thread_sp)
223 {
224 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
225 }
226 }
227 return frame_sp;
228}
229
230//------------------------------------------------------------------
231// Thread class
232//------------------------------------------------------------------
233
234ConstString &
235Thread::GetStaticBroadcasterClass ()
236{
237 static ConstString class_name ("lldb.thread");
238 return class_name;
239}
240
241Thread::Thread (Process &process, lldb::tid_t tid) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000242 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000244 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
245 m_process_wp (process.shared_from_this()),
Greg 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{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000285 // Tell any plans on the plan stack that the thread is being destroyed since
286 // any active plans that have a thread go away in the middle of might need
287 // to do cleanup.
288 for (auto plan : m_plan_stack)
289 plan->ThreadDestroyed();
290
Greg Clayton35a4cc52012-10-29 20:52:08 +0000291 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000292 m_plan_stack.clear();
293 m_discarded_plan_stack.clear();
294 m_completed_plan_stack.clear();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000295 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000296 m_reg_context_sp.reset();
297 m_unwinder_ap.reset();
298 Mutex::Locker locker(m_frame_mutex);
299 m_curr_frames_sp.reset();
300 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301}
302
Jim Ingham4f465cf2012-10-10 18:32:14 +0000303void
304Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
305{
306 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
307 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
308}
309
310uint32_t
311Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
312{
313 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
314 if (broadcast)
315 BroadcastSelectedFrameChange(frame->GetStackID());
316 return ret_value;
317}
318
319bool
320Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
321{
322 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
323 if (frame_sp)
324 {
325 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
326 if (broadcast)
327 BroadcastSelectedFrameChange(frame_sp->GetStackID());
328 return true;
329 }
330 else
331 return false;
332}
333
Jim Ingham93208b82013-01-31 21:46:01 +0000334bool
335Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
336{
337 const bool broadcast = true;
338 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
339 if (success)
340 {
341 StackFrameSP frame_sp = GetSelectedFrame();
342 if (frame_sp)
343 {
344 bool already_shown = false;
345 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
346 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
347 {
348 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
349 }
350
351 bool show_frame_info = true;
352 bool show_source = !already_shown;
353 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
354 }
355 return false;
356 }
357 else
358 return false;
359}
360
Jim Ingham4f465cf2012-10-10 18:32:14 +0000361
Jim Inghamb15bfc72010-10-20 00:39:53 +0000362lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000363Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364{
Jim Inghamb15bfc72010-10-20 00:39:53 +0000365 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000366 ProcessSP process_sp (GetProcess());
367 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000368 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000369 {
Jim Ingham73ca05a2011-12-17 01:35:57 +0000370 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000371 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000372 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000373 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000374 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
375 (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 +0000376 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000377 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000378 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000379 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000380 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000381 GetPrivateStopInfo ();
382 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000383 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000384 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385}
386
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000387lldb::StopInfoSP
388Thread::GetPrivateStopInfo ()
389{
390 ProcessSP process_sp (GetProcess());
391 if (process_sp)
392 {
393 ProcessSP process_sp (GetProcess());
394 if (process_sp)
395 {
396 const uint32_t process_stop_id = process_sp->GetStopID();
397 if (m_stop_info_stop_id != process_stop_id)
398 {
399 if (m_stop_info_sp)
400 {
401 if (m_stop_info_sp->IsValid())
402 {
403 SetStopInfo (m_stop_info_sp);
404 }
405 else
406 {
407 if (IsStillAtLastBreakpointHit())
408 SetStopInfo(m_stop_info_sp);
409 else
410 m_stop_info_sp.reset();
411 }
412 }
413
414 if (!m_stop_info_sp)
415 {
416 if (CalculateStopInfo() == false)
417 SetStopInfo (StopInfoSP());
418 }
419 }
420 }
421 }
422 return m_stop_info_sp;
423}
424
425
Greg Clayton97d5cf02012-09-25 02:40:06 +0000426lldb::StopReason
427Thread::GetStopReason()
428{
429 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
430 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000431 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000432 return eStopReasonNone;
433}
434
435
436
Jim Ingham77787032011-01-20 02:03:18 +0000437void
438Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
439{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000440 m_stop_info_sp = stop_info_sp;
441 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000442 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000443 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000444 // If we are overriding the ShouldReportStop, do that here:
445 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000446 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000447 }
448
Greg Clayton1ac04c32012-02-21 00:09:25 +0000449 ProcessSP process_sp (GetProcess());
450 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000451 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000452 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000453 m_stop_info_stop_id = UINT32_MAX;
Jim Ingham77787032011-01-20 02:03:18 +0000454}
455
456void
Jim Ingham221d51c2013-05-08 00:35:16 +0000457Thread::SetShouldReportStop (Vote vote)
458{
459 if (vote == eVoteNoOpinion)
460 return;
461 else
462 {
463 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000464 if (m_stop_info_sp)
465 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000466 }
467}
468
469void
Jim Ingham77787032011-01-20 02:03:18 +0000470Thread::SetStopInfoToNothing()
471{
472 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
473 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
474 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
475}
476
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477bool
478Thread::ThreadStoppedForAReason (void)
479{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000480 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481}
482
Jim Ingham77787032011-01-20 02:03:18 +0000483bool
484Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
485{
486 if (!SaveFrameZeroState(saved_state.register_backup))
487 return false;
488
489 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000490 ProcessSP process_sp (GetProcess());
491 if (process_sp)
492 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000493 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
494
Jim Ingham77787032011-01-20 02:03:18 +0000495 return true;
496}
497
498bool
Jim Ingham8559a352012-11-26 23:52:18 +0000499Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000500{
501 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham8559a352012-11-26 23:52:18 +0000502 return true;
503}
504
505bool
506Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
507{
Jim Ingham0c270682011-01-25 02:47:23 +0000508 if (saved_state.stop_info_sp)
509 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000510 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000511 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000512 return true;
513}
514
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515StateType
516Thread::GetState() const
517{
518 // If any other threads access this we will need a mutex for it
519 Mutex::Locker locker(m_state_mutex);
520 return m_state;
521}
522
523void
524Thread::SetState(StateType state)
525{
526 Mutex::Locker locker(m_state_mutex);
527 m_state = state;
528}
529
530void
531Thread::WillStop()
532{
533 ThreadPlan *current_plan = GetCurrentPlan();
534
535 // FIXME: I may decide to disallow threads with no plans. In which
536 // case this should go to an assert.
537
538 if (!current_plan)
539 return;
540
541 current_plan->WillStop();
542}
543
544void
545Thread::SetupForResume ()
546{
547 if (GetResumeState() != eStateSuspended)
548 {
549
550 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
551 // telling the current plan it will resume, since we might change what the current
552 // plan is.
553
Greg Clayton1afa68e2013-04-02 20:32:37 +0000554// StopReason stop_reason = lldb::eStopReasonInvalid;
555// StopInfoSP stop_info_sp = GetStopInfo();
556// if (stop_info_sp.get())
557// stop_reason = stop_info_sp->GetStopReason();
558// if (stop_reason == lldb::eStopReasonBreakpoint)
559 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
560 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000562 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
563 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000564 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000565 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
566 // special to step over a breakpoint.
567
568 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000569
Greg Clayton1afa68e2013-04-02 20:32:37 +0000570 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
571 {
572 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
573 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000575 ThreadPlanSP step_bp_plan_sp;
576 step_bp_plan->SetPrivate (true);
577
578 if (GetCurrentPlan()->RunState() != eStateStepping)
579 {
580 step_bp_plan->SetAutoContinue(true);
581 }
582 step_bp_plan_sp.reset (step_bp_plan);
583 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 }
586 }
587 }
588 }
589}
590
591bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000592Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593{
594 // At this point clear the completed plan stack.
595 m_completed_plan_stack.clear();
596 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000597 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598
Greg Clayton97d5cf02012-09-25 02:40:06 +0000599 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000600
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000601 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
602 if (backing_thread_sp)
603 backing_thread_sp->m_temporary_resume_state = resume_state;
604
605 // Make sure m_stop_info_sp is valid
606 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000607
Jim Ingham92087d82012-01-31 23:09:20 +0000608 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
609 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
610 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
611 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000612 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000613 if (m_stop_info_stop_id == process_stop_id &&
614 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000615 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000616 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000617 if (stop_info)
618 stop_info->WillResume (resume_state);
619 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620
621 // Tell all the plans that we are about to resume in case they need to clear any state.
622 // We distinguish between the plan on the top of the stack and the lower
623 // plans in case a plan needs to do any special business before it runs.
624
Greg Claytond1d06e42013-04-20 00:27:58 +0000625 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000627 if (plan_ptr)
628 {
629 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Greg Claytond1d06e42013-04-20 00:27:58 +0000631 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
632 {
633 plan_ptr->WillResume (resume_state, false);
634 }
635
636 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
637 // In that case, don't reset it here.
638
639 if (need_to_resume && resume_state != eStateSuspended)
640 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000641 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000642 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000643 }
644
Greg Clayton160c9d82013-05-01 21:54:04 +0000645 if (need_to_resume)
646 {
647 ClearStackFrames();
648 // Let Thread subclasses do any special work they need to prior to resuming
649 WillResume (resume_state);
650 }
651
Jim Ingham513c6bb2012-09-01 01:02:41 +0000652 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653}
654
655void
656Thread::DidResume ()
657{
658 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
659}
660
661bool
662Thread::ShouldStop (Event* event_ptr)
663{
664 ThreadPlan *current_plan = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000665
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 bool should_stop = true;
667
Greg Clayton5160ce52013-03-27 23:08:40 +0000668 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000669
670 if (GetResumeState () == eStateSuspended)
671 {
672 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000673 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 +0000674 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000675 GetID (),
676 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000677 return false;
678 }
679
680 if (GetTemporaryResumeState () == eStateSuspended)
681 {
682 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000683 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 +0000684 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000685 GetID (),
686 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000687 return false;
688 }
689
690 if (ThreadStoppedForAReason() == false)
691 {
692 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000693 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 +0000694 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000695 GetID (),
696 GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000697 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000698 return false;
699 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000700
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 if (log)
702 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000703 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Jim Ingham92087d82012-01-31 23:09:20 +0000704 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000705 GetID (),
706 GetProtocolID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000707 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000708 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000710 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000712 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000714
715 // The top most plan always gets to do the trace log...
716 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000717
718 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
719 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
720 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000721 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000722 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
723 {
724 if (log)
725 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
726 return false;
727 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
Jim Inghambad39e42012-09-05 21:12:49 +0000729 // If we've already been restarted, don't query the plans since the state they would examine is not current.
730 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
731 return false;
732
733 // Before the plans see the state of the world, calculate the current inlined depth.
734 GetStackFrameList()->CalculateCurrentInlinedDepth();
735
Jim Ingham25f66702011-12-03 01:52:59 +0000736 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
737 // If that plan is still working, then we don't need to do any more work. If the plan that explains
738 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
739 // whether they still need to do more work.
740
741 bool done_processing_current_plan = false;
742
Jim Ingham0161b492013-02-09 01:29:05 +0000743 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000744 {
745 if (current_plan->TracerExplainsStop())
746 {
747 done_processing_current_plan = true;
748 should_stop = false;
749 }
750 else
751 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000752 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000753 // does and let it handle the situation.
754 ThreadPlan *plan_ptr = current_plan;
755 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
756 {
Jim Ingham0161b492013-02-09 01:29:05 +0000757 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000758 {
759 should_stop = plan_ptr->ShouldStop (event_ptr);
760
761 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
762 // and all the plans below it off the stack.
763
764 if (plan_ptr->MischiefManaged())
765 {
Jim Ingham031177e2012-05-11 23:49:49 +0000766 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000767 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000768
769 do
770 {
771 if (should_stop)
772 current_plan->WillStop();
773 PopPlan();
774 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000775 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
776 // Now, if the responsible plan was not "Okay to discard" then we're done,
777 // otherwise we forward this to the next plan in the stack below.
778 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
779 done_processing_current_plan = true;
780 else
781 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000782 }
783 else
784 done_processing_current_plan = true;
785
786 break;
787 }
788
789 }
790 }
791 }
792
793 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000795 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000796
Jim Ingham10c4b242011-10-15 00:23:43 +0000797 if (log)
798 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
799
Jim Ingham0f16e732011-02-08 05:20:59 +0000800 // We're starting from the base plan, so just let it decide;
801 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000803 should_stop = current_plan->ShouldStop (event_ptr);
804 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000805 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000806 }
807 else
808 {
809 // Otherwise, don't let the base plan override what the other plans say to do, since
810 // presumably if there were other plans they would know what to do...
811 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000813 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000815
816 should_stop = current_plan->ShouldStop(event_ptr);
817 if (log)
818 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
819 if (current_plan->MischiefManaged())
820 {
821 if (should_stop)
822 current_plan->WillStop();
823
824 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
825 // Otherwise, see if the plan's parent wants to stop.
826
827 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
828 {
829 PopPlan();
830 break;
831 }
832 else
833 {
834
835 PopPlan();
836
837 current_plan = GetCurrentPlan();
838 if (current_plan == NULL)
839 {
840 break;
841 }
842 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843 }
844 else
845 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000846 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 }
849 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000850
Jim Inghamb01e7422010-06-19 04:45:32 +0000851 if (over_ride_stop)
852 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000853
854 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
855 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
856 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
857 // This code clears stale plans off the stack.
858
859 if (should_stop)
860 {
861 ThreadPlan *plan_ptr = GetCurrentPlan();
862 while (!PlanIsBasePlan(plan_ptr))
863 {
864 bool stale = plan_ptr->IsPlanStale ();
865 ThreadPlan *examined_plan = plan_ptr;
866 plan_ptr = GetPreviousPlan (examined_plan);
867
868 if (stale)
869 {
870 if (log)
871 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
872 DiscardThreadPlansUpToPlan(examined_plan);
873 }
874 }
875 }
876
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878
Jim Ingham10c4b242011-10-15 00:23:43 +0000879 if (log)
880 {
881 StreamString s;
882 s.IndentMore();
883 DumpThreadPlans(&s);
884 log->Printf ("Plan stack final state:\n%s", s.GetData());
885 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
886 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887 return should_stop;
888}
889
890Vote
891Thread::ShouldReportStop (Event* event_ptr)
892{
893 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000894 StateType temp_thread_state = GetTemporaryResumeState();
895
Greg Clayton5160ce52013-03-27 23:08:40 +0000896 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000897
898 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
899 {
900 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000901 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 +0000902 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000903 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904
Jim Ingham92087d82012-01-31 23:09:20 +0000905 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
906 {
907 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000908 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 +0000909 return eVoteNoOpinion;
910 }
911
912 if (!ThreadStoppedForAReason())
913 {
914 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000915 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 +0000916 return eVoteNoOpinion;
917 }
918
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 if (m_completed_plan_stack.size() > 0)
920 {
921 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000922 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000923 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 +0000924 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
925 }
926 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000927 {
Jim Ingham0161b492013-02-09 01:29:05 +0000928 Vote thread_vote = eVoteNoOpinion;
929 ThreadPlan *plan_ptr = GetCurrentPlan();
930 while (1)
931 {
932 if (plan_ptr->PlanExplainsStop(event_ptr))
933 {
934 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
935 break;
936 }
937 if (PlanIsBasePlan(plan_ptr))
938 break;
939 else
940 plan_ptr = GetPreviousPlan(plan_ptr);
941 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000942 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000943 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 +0000944
945 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000946 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947}
948
949Vote
950Thread::ShouldReportRun (Event* event_ptr)
951{
952 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000953
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954 if (thread_state == eStateSuspended
955 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000956 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000958 }
959
Greg Clayton5160ce52013-03-27 23:08:40 +0000960 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 if (m_completed_plan_stack.size() > 0)
962 {
963 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000964 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000965 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000966 GetIndexID(),
967 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000968 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000969 m_completed_plan_stack.back()->GetName());
970
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
972 }
973 else
Jim Ingham444586b2011-01-24 06:34:17 +0000974 {
975 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000976 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000977 GetIndexID(),
978 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000979 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000980 GetCurrentPlan()->GetName());
981
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000983 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984}
985
Jim Ingham1b54c882010-06-16 02:00:15 +0000986bool
987Thread::MatchesSpec (const ThreadSpec *spec)
988{
989 if (spec == NULL)
990 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000991
Jim Ingham3d902922012-03-07 22:03:04 +0000992 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +0000993}
994
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995void
996Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
997{
998 if (thread_plan_sp)
999 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001000 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1001 if (!thread_plan_sp->GetThreadPlanTracer())
1002 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001003 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +00001004
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005 thread_plan_sp->DidPush();
1006
Greg Clayton5160ce52013-03-27 23:08:40 +00001007 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008 if (log)
1009 {
1010 StreamString s;
1011 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Daniel Malead01b2952012-11-29 21:49:15 +00001012 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001014 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 }
1016 }
1017}
1018
1019void
1020Thread::PopPlan ()
1021{
Greg Clayton5160ce52013-03-27 23:08:40 +00001022 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001024 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025 return;
1026 else
1027 {
1028 ThreadPlanSP &plan = m_plan_stack.back();
1029 if (log)
1030 {
Daniel Malead01b2952012-11-29 21:49:15 +00001031 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001032 }
1033 m_completed_plan_stack.push_back (plan);
1034 plan->WillPop();
1035 m_plan_stack.pop_back();
1036 }
1037}
1038
1039void
1040Thread::DiscardPlan ()
1041{
1042 if (m_plan_stack.size() > 1)
1043 {
1044 ThreadPlanSP &plan = m_plan_stack.back();
1045 m_discarded_plan_stack.push_back (plan);
1046 plan->WillPop();
1047 m_plan_stack.pop_back();
1048 }
1049}
1050
1051ThreadPlan *
1052Thread::GetCurrentPlan ()
1053{
Jim Inghame1471232012-04-19 00:17:05 +00001054 // There will always be at least the base plan. If somebody is mucking with a
1055 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001056 if (m_plan_stack.empty())
1057 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001058 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059}
1060
1061ThreadPlanSP
1062Thread::GetCompletedPlan ()
1063{
1064 ThreadPlanSP empty_plan_sp;
1065 if (!m_completed_plan_stack.empty())
1066 {
1067 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1068 {
1069 ThreadPlanSP completed_plan_sp;
1070 completed_plan_sp = m_completed_plan_stack[i];
1071 if (!completed_plan_sp->GetPrivate ())
1072 return completed_plan_sp;
1073 }
1074 }
1075 return empty_plan_sp;
1076}
1077
Jim Ingham73ca05a2011-12-17 01:35:57 +00001078ValueObjectSP
1079Thread::GetReturnValueObject ()
1080{
1081 if (!m_completed_plan_stack.empty())
1082 {
1083 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1084 {
1085 ValueObjectSP return_valobj_sp;
1086 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1087 if (return_valobj_sp)
1088 return return_valobj_sp;
1089 }
1090 }
1091 return ValueObjectSP();
1092}
1093
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094bool
1095Thread::IsThreadPlanDone (ThreadPlan *plan)
1096{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097 if (!m_completed_plan_stack.empty())
1098 {
1099 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1100 {
1101 if (m_completed_plan_stack[i].get() == plan)
1102 return true;
1103 }
1104 }
1105 return false;
1106}
1107
1108bool
1109Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1110{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 if (!m_discarded_plan_stack.empty())
1112 {
1113 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1114 {
1115 if (m_discarded_plan_stack[i].get() == plan)
1116 return true;
1117 }
1118 }
1119 return false;
1120}
1121
1122ThreadPlan *
1123Thread::GetPreviousPlan (ThreadPlan *current_plan)
1124{
1125 if (current_plan == NULL)
1126 return NULL;
1127
1128 int stack_size = m_completed_plan_stack.size();
1129 for (int i = stack_size - 1; i > 0; i--)
1130 {
1131 if (current_plan == m_completed_plan_stack[i].get())
1132 return m_completed_plan_stack[i-1].get();
1133 }
1134
1135 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1136 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137 if (m_plan_stack.size() > 0)
1138 return m_plan_stack.back().get();
1139 else
1140 return NULL;
1141 }
1142
1143 stack_size = m_plan_stack.size();
1144 for (int i = stack_size - 1; i > 0; i--)
1145 {
1146 if (current_plan == m_plan_stack[i].get())
1147 return m_plan_stack[i-1].get();
1148 }
1149 return NULL;
1150}
1151
1152void
1153Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1154{
1155 if (abort_other_plans)
1156 DiscardThreadPlans(true);
1157
1158 PushPlan (thread_plan_sp);
1159}
1160
Jim Ingham06e827c2010-11-11 19:26:09 +00001161
1162void
1163Thread::EnableTracer (bool value, bool single_stepping)
1164{
1165 int stack_size = m_plan_stack.size();
1166 for (int i = 0; i < stack_size; i++)
1167 {
1168 if (m_plan_stack[i]->GetThreadPlanTracer())
1169 {
1170 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1171 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1172 }
1173 }
1174}
1175
1176void
1177Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1178{
1179 int stack_size = m_plan_stack.size();
1180 for (int i = 0; i < stack_size; i++)
1181 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1182}
1183
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001185Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1186{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001187 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1188}
1189
1190void
1191Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1192{
Greg Clayton5160ce52013-03-27 23:08:40 +00001193 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001194 if (log)
1195 {
Daniel Malead01b2952012-11-29 21:49:15 +00001196 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 +00001197 }
1198
1199 int stack_size = m_plan_stack.size();
1200
1201 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1202 // stack, and if so discard up to and including it.
1203
Jim Ingham64e7ead2012-05-03 21:19:36 +00001204 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001205 {
1206 for (int i = stack_size - 1; i > 0; i--)
1207 DiscardPlan();
1208 }
1209 else
1210 {
1211 bool found_it = false;
1212 for (int i = stack_size - 1; i > 0; i--)
1213 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001214 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001215 found_it = true;
1216 }
1217 if (found_it)
1218 {
1219 bool last_one = false;
1220 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1221 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001222 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001223 last_one = true;
1224 DiscardPlan();
1225 }
1226 }
1227 }
1228 return;
1229}
1230
1231void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001232Thread::DiscardThreadPlans(bool force)
1233{
Greg Clayton5160ce52013-03-27 23:08:40 +00001234 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235 if (log)
1236 {
Daniel Malead01b2952012-11-29 21:49:15 +00001237 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238 }
1239
1240 if (force)
1241 {
1242 int stack_size = m_plan_stack.size();
1243 for (int i = stack_size - 1; i > 0; i--)
1244 {
1245 DiscardPlan();
1246 }
1247 return;
1248 }
1249
1250 while (1)
1251 {
1252
1253 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001254 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255
1256 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1257 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1258 {
1259 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1260 {
1261 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1262 break;
1263 }
1264 }
1265
1266 if (discard)
1267 {
1268 // First pop all the dependent plans:
1269 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1270 {
1271
1272 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1273 // for the plan leaves it in a state that it is safe to pop the plan
1274 // with no more notice?
1275 DiscardPlan();
1276 }
1277
1278 // Now discard the master plan itself.
1279 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1280 // discard it's dependent plans, but not it...
1281 if (master_plan_idx > 0)
1282 {
1283 DiscardPlan();
1284 }
1285 }
1286 else
1287 {
1288 // If the master plan doesn't want to get discarded, then we're done.
1289 break;
1290 }
1291
1292 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293}
1294
Jim Inghamcf274f92012-04-09 22:37:39 +00001295bool
1296Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1297{
1298 if (plan_ptr->IsBasePlan())
1299 return true;
1300 else if (m_plan_stack.size() == 0)
1301 return false;
1302 else
1303 return m_plan_stack[0].get() == plan_ptr;
1304}
1305
Jim Ingham93208b82013-01-31 21:46:01 +00001306Error
1307Thread::UnwindInnermostExpression()
1308{
1309 Error error;
1310 int stack_size = m_plan_stack.size();
1311
1312 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1313 // stack, and if so discard up to and including it.
1314
1315 for (int i = stack_size - 1; i > 0; i--)
1316 {
1317 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1318 {
1319 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1320 return error;
1321 }
1322 }
1323 error.SetErrorString("No expressions currently active on this thread");
1324 return error;
1325}
1326
1327
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328ThreadPlan *
1329Thread::QueueFundamentalPlan (bool abort_other_plans)
1330{
1331 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1332 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1333 return thread_plan_sp.get();
1334}
1335
1336ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001337Thread::QueueThreadPlanForStepSingleInstruction
1338(
1339 bool step_over,
1340 bool abort_other_plans,
1341 bool stop_other_threads
1342)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343{
1344 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1345 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1346 return thread_plan_sp.get();
1347}
1348
1349ThreadPlan *
Jim Inghamc6276822012-12-12 19:58:40 +00001350Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001351(
1352 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001353 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001354 const SymbolContext &addr_context,
1355 lldb::RunMode stop_other_threads
1356)
1357{
1358 ThreadPlanSP thread_plan_sp;
1359 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1360
1361 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1362 return thread_plan_sp.get();
1363}
1364
1365ThreadPlan *
1366Thread::QueueThreadPlanForStepInRange
1367(
1368 bool abort_other_plans,
1369 const AddressRange &range,
1370 const SymbolContext &addr_context,
1371 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001372 lldb::RunMode stop_other_threads,
1373 bool avoid_code_without_debug_info
1374)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375{
1376 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001377 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1378 if (avoid_code_without_debug_info)
1379 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380 else
Jim Inghamc6276822012-12-12 19:58:40 +00001381 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1382 if (step_in_target)
1383 plan->SetStepInTarget(step_in_target);
1384 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385
1386 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1387 return thread_plan_sp.get();
1388}
1389
1390
1391ThreadPlan *
1392Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1393{
1394 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1395 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1396 return thread_plan_sp.get();
1397}
1398
1399ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001400Thread::QueueThreadPlanForStepOut
1401(
1402 bool abort_other_plans,
1403 SymbolContext *addr_context,
1404 bool first_insn,
1405 bool stop_other_threads,
1406 Vote stop_vote,
1407 Vote run_vote,
1408 uint32_t frame_idx
1409)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410{
Greg Clayton481cef22011-01-21 06:11:58 +00001411 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1412 addr_context,
1413 first_insn,
1414 stop_other_threads,
1415 stop_vote,
1416 run_vote,
1417 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001418
1419 if (thread_plan_sp->ValidatePlan(NULL))
1420 {
1421 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1422 return thread_plan_sp.get();
1423 }
1424 else
1425 {
1426 return NULL;
1427 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428}
1429
1430ThreadPlan *
Jim Ingham18de2fd2012-05-10 01:35:39 +00001431Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001433 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001434 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1435 return NULL;
1436
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1438 return thread_plan_sp.get();
1439}
1440
1441ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1443 Address& function,
1444 lldb::addr_t arg,
1445 bool stop_other_threads,
Jim Ingham184e9812013-01-15 02:47:48 +00001446 bool unwind_on_error,
1447 bool ignore_breakpoints)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448{
Jim Ingham184e9812013-01-15 02:47:48 +00001449 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1450 function,
1451 ClangASTType(),
1452 arg,
1453 stop_other_threads,
1454 unwind_on_error,
1455 ignore_breakpoints));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1457 return thread_plan_sp.get();
1458}
1459
1460ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1462 Address &target_addr,
1463 bool stop_other_threads)
1464{
1465 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1466 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1467 return thread_plan_sp.get();
1468}
1469
1470ThreadPlan *
1471Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001472 lldb::addr_t *address_list,
1473 size_t num_addresses,
1474 bool stop_other_threads,
1475 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476{
Greg Clayton481cef22011-01-21 06:11:58 +00001477 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1479 return thread_plan_sp.get();
1480
1481}
1482
1483uint32_t
1484Thread::GetIndexID () const
1485{
1486 return m_index_id;
1487}
1488
1489void
1490Thread::DumpThreadPlans (lldb_private::Stream *s) const
1491{
1492 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001493 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001494 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001495 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 +00001496 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001499 s->Indent();
1500 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001503 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504 }
1505
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001507 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001509 s->Indent();
1510 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1511 for (i = stack_size - 1; i >= 0; i--)
1512 {
1513 s->IndentMore();
1514 s->Indent();
1515 s->Printf ("Element %d: ", i);
1516 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1517 s->EOL();
1518 s->IndentLess();
1519 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 }
1521
1522 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001523 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001525 s->Indent();
1526 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1527 for (i = stack_size - 1; i >= 0; i--)
1528 {
1529 s->IndentMore();
1530 s->Indent();
1531 s->Printf ("Element %d: ", i);
1532 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1533 s->EOL();
1534 s->IndentLess();
1535 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001536 }
1537
1538}
1539
Greg Claytond9e416c2012-02-18 05:35:26 +00001540TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001541Thread::CalculateTarget ()
1542{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001543 TargetSP target_sp;
1544 ProcessSP process_sp(GetProcess());
1545 if (process_sp)
1546 target_sp = process_sp->CalculateTarget();
1547 return target_sp;
1548
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549}
1550
Greg Claytond9e416c2012-02-18 05:35:26 +00001551ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001552Thread::CalculateProcess ()
1553{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001554 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001555}
1556
Greg Claytond9e416c2012-02-18 05:35:26 +00001557ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001558Thread::CalculateThread ()
1559{
Greg Claytond9e416c2012-02-18 05:35:26 +00001560 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561}
1562
Greg Claytond9e416c2012-02-18 05:35:26 +00001563StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564Thread::CalculateStackFrame ()
1565{
Greg Claytond9e416c2012-02-18 05:35:26 +00001566 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567}
1568
1569void
Greg Clayton0603aa92010-10-04 01:05:56 +00001570Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001572 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573}
1574
Greg Clayton12daf9462010-08-25 00:35:26 +00001575
Greg Clayton39d0ab32012-03-29 01:41:38 +00001576StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001577Thread::GetStackFrameList ()
1578{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001579 StackFrameListSP frame_list_sp;
1580 Mutex::Locker locker(m_frame_mutex);
1581 if (m_curr_frames_sp)
1582 {
1583 frame_list_sp = m_curr_frames_sp;
1584 }
1585 else
1586 {
1587 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1588 m_curr_frames_sp = frame_list_sp;
1589 }
1590 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001591}
1592
Greg Clayton12daf9462010-08-25 00:35:26 +00001593void
1594Thread::ClearStackFrames ()
1595{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001596 Mutex::Locker locker(m_frame_mutex);
1597
Greg Clayton160c9d82013-05-01 21:54:04 +00001598 Unwind *unwinder = GetUnwinder ();
1599 if (unwinder)
1600 unwinder->Clear();
1601
Jim Inghamb0c72a52012-02-29 03:40:22 +00001602 // Only store away the old "reference" StackFrameList if we got all its frames:
1603 // FIXME: At some point we can try to splice in the frames we have fetched into
1604 // the new frame as we make it, but let's not try that now.
1605 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001606 m_prev_frames_sp.swap (m_curr_frames_sp);
1607 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001608}
1609
1610lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001611Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1612{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001613 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001614}
1615
Jim Ingham44137582012-09-12 00:40:39 +00001616
1617Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001618Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001619{
1620 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1621 Error return_error;
1622
1623 if (!frame_sp)
1624 {
Daniel Malead01b2952012-11-29 21:49:15 +00001625 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001626 }
1627
Jim Ingham4f465cf2012-10-10 18:32:14 +00001628 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001629}
1630
1631Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001632Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001633{
1634 Error return_error;
1635
1636 if (!frame_sp)
1637 {
1638 return_error.SetErrorString("Can't return to a null frame.");
1639 return return_error;
1640 }
1641
1642 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001643 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1644 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001645 if (!older_frame_sp)
1646 {
1647 return_error.SetErrorString("No older frame to return to.");
1648 return return_error;
1649 }
Jim Ingham44137582012-09-12 00:40:39 +00001650
1651 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001652 {
Jim Ingham44137582012-09-12 00:40:39 +00001653 lldb::ABISP abi = thread->GetProcess()->GetABI();
1654 if (!abi)
1655 {
1656 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001657 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001658 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001659 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1660
1661 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1662 // Turn that back on when that works.
1663 if (0 && sc.function != NULL)
1664 {
1665 Type *function_type = sc.function->GetType();
1666 if (function_type)
1667 {
1668 clang_type_t return_type = sc.function->GetReturnClangType();
1669 if (return_type)
1670 {
1671 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1672 StreamString s;
1673 ast_type.DumpTypeDescription(&s);
1674 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1675 if (cast_value_sp)
1676 {
1677 cast_value_sp->SetFormat(eFormatHex);
1678 return_value_sp = cast_value_sp;
1679 }
1680 }
1681 }
1682 }
1683
Jim Inghamcb640dd2012-09-14 02:14:15 +00001684 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001685 if (!return_error.Success())
1686 return return_error;
1687 }
1688
1689 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001690 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001691 // cook their data
1692
1693 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1694 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001695 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001696 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1697 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001698 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001699 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1700 if (copy_success)
1701 {
1702 thread->DiscardThreadPlans(true);
1703 thread->ClearStackFrames();
1704 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1705 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1706 }
1707 else
1708 {
1709 return_error.SetErrorString("Could not reset register values.");
1710 }
Jim Ingham93208b82013-01-31 21:46:01 +00001711 }
1712 else
1713 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001714 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001715 }
Jim Ingham44137582012-09-12 00:40:39 +00001716 }
1717 else
1718 {
Jim Ingham93208b82013-01-31 21:46:01 +00001719 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001720 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001721 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001722}
1723
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724void
Greg Clayton0603aa92010-10-04 01:05:56 +00001725Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001727 ExecutionContext exe_ctx (shared_from_this());
1728 Process *process = exe_ctx.GetProcessPtr();
1729 if (process == NULL)
1730 return;
1731
Greg Claytonc14ee322011-09-22 04:58:26 +00001732 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001733 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001734 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001736 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001737 if (frame_sp)
1738 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001739 exe_ctx.SetFrameSP(frame_sp);
1740 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741 }
1742 }
1743
Greg Clayton1ac04c32012-02-21 00:09:25 +00001744 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001745 assert (thread_format);
1746 const char *end = NULL;
1747 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001748 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001749 &exe_ctx,
1750 NULL,
1751 strm,
1752 &end);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753}
1754
Greg Clayton99d0faf2010-11-18 23:32:35 +00001755void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001756Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001757{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001758}
Jim Inghamee8aea12010-09-08 03:14:33 +00001759
Greg Clayton99d0faf2010-11-18 23:32:35 +00001760void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001761Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001762{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001763}
Jim Inghamee8aea12010-09-08 03:14:33 +00001764
Jim Inghame4284b72010-09-23 17:40:12 +00001765lldb::StackFrameSP
1766Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1767{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001768 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001769}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001770
1771const char *
1772Thread::StopReasonAsCString (lldb::StopReason reason)
1773{
1774 switch (reason)
1775 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001776 case eStopReasonInvalid: return "invalid";
1777 case eStopReasonNone: return "none";
1778 case eStopReasonTrace: return "trace";
1779 case eStopReasonBreakpoint: return "breakpoint";
1780 case eStopReasonWatchpoint: return "watchpoint";
1781 case eStopReasonSignal: return "signal";
1782 case eStopReasonException: return "exception";
1783 case eStopReasonExec: return "exec";
1784 case eStopReasonPlanComplete: return "plan complete";
1785 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001786 }
1787
1788
1789 static char unknown_state_string[64];
1790 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1791 return unknown_state_string;
1792}
1793
1794const char *
1795Thread::RunModeAsCString (lldb::RunMode mode)
1796{
1797 switch (mode)
1798 {
1799 case eOnlyThisThread: return "only this thread";
1800 case eAllThreads: return "all threads";
1801 case eOnlyDuringStepping: return "only during stepping";
1802 }
1803
1804 static char unknown_state_string[64];
1805 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1806 return unknown_state_string;
1807}
Jim Ingham06e827c2010-11-11 19:26:09 +00001808
Greg Clayton7260f622011-04-18 08:33:37 +00001809size_t
1810Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1811{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001812 ExecutionContext exe_ctx (shared_from_this());
1813 Target *target = exe_ctx.GetTargetPtr();
1814 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001815 size_t num_frames_shown = 0;
1816 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001817 bool is_selected = false;
1818 if (process)
1819 {
1820 if (process->GetThreadList().GetSelectedThread().get() == this)
1821 is_selected = true;
1822 }
1823 strm.Printf("%c ", is_selected ? '*' : ' ');
1824 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001825 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001826 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001827 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001828 {
Jim Inghame610d642011-08-16 00:07:28 +00001829 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1830 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1831 {
1832 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1833 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001834 }
Greg Clayton7260f622011-04-18 08:33:37 +00001835 }
1836
1837 DumpUsingSettingsFormat (strm, start_frame);
1838
1839 if (num_frames > 0)
1840 {
1841 strm.IndentMore();
1842
1843 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001844 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001845 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1846 start_frame,
1847 num_frames,
1848 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001849 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001850 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001851 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001852 }
1853 return num_frames_shown;
1854}
1855
1856size_t
1857Thread::GetStackFrameStatus (Stream& strm,
1858 uint32_t first_frame,
1859 uint32_t num_frames,
1860 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001861 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001862{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001863 return GetStackFrameList()->GetStatus (strm,
1864 first_frame,
1865 num_frames,
1866 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001867 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001868}
1869
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001870bool
1871Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1872{
1873 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1874 if (frame_sp)
1875 {
1876 checkpoint.SetStackID(frame_sp->GetStackID());
Greg Clayton1afa68e2013-04-02 20:32:37 +00001877 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1878 if (reg_ctx_sp)
1879 return reg_ctx_sp->ReadAllRegisterValues (checkpoint.GetData());
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001880 }
1881 return false;
1882}
Greg Clayton7260f622011-04-18 08:33:37 +00001883
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001884bool
1885Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1886{
Jim Ingham44137582012-09-12 00:40:39 +00001887 return ResetFrameZeroRegisters (checkpoint.GetData());
1888}
1889
1890bool
1891Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1892{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001893 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1894 if (frame_sp)
1895 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001896 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1897 if (reg_ctx_sp)
1898 {
1899 bool ret = reg_ctx_sp->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001900
Greg Clayton1afa68e2013-04-02 20:32:37 +00001901 // Clear out all stack frames as our world just changed.
1902 ClearStackFrames();
1903 reg_ctx_sp->InvalidateIfNeeded(true);
1904 if (m_unwinder_ap.get())
1905 m_unwinder_ap->Clear();
1906 return ret;
1907 }
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001908 }
1909 return false;
1910}
Greg Clayton7260f622011-04-18 08:33:37 +00001911
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001912Unwind *
1913Thread::GetUnwinder ()
1914{
1915 if (m_unwinder_ap.get() == NULL)
1916 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001917 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001918 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1919 switch (machine)
1920 {
1921 case llvm::Triple::x86_64:
1922 case llvm::Triple::x86:
1923 case llvm::Triple::arm:
1924 case llvm::Triple::thumb:
1925 m_unwinder_ap.reset (new UnwindLLDB (*this));
1926 break;
1927
1928 default:
1929 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1930 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1931 break;
1932 }
1933 }
1934 return m_unwinder_ap.get();
1935}
1936
1937
Greg Claytonfa559e52012-05-18 02:38:05 +00001938void
1939Thread::Flush ()
1940{
1941 ClearStackFrames ();
1942 m_reg_context_sp.reset();
1943}
Jim Ingham5d88a062012-10-16 00:09:33 +00001944
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00001945bool
Jim Ingham5d88a062012-10-16 00:09:33 +00001946Thread::IsStillAtLastBreakpointHit ()
1947{
1948 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1949 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1950 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001951 if (m_stop_info_sp) {
1952 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00001953 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001954 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00001955 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
1956 if (reg_ctx_sp)
1957 {
1958 lldb::addr_t pc = reg_ctx_sp->GetPC();
1959 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1960 if (bp_site_sp && value == bp_site_sp->GetID())
1961 return true;
1962 }
Jim Ingham5d88a062012-10-16 00:09:33 +00001963 }
1964 }
1965 return false;
1966}