]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/ExecutionContext.cpp
Update lld to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / ExecutionContext.cpp
1 //===-- ExecutionContext.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
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/Target/ExecutionContext.h"
15 #include "lldb/Core/State.h"
16 #include "lldb/Target/ExecutionContextScope.h"
17 #include "lldb/Target/StackFrame.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Target/Thread.h"
21
22 using namespace lldb_private;
23
24 ExecutionContext::ExecutionContext() :
25     m_target_sp (),
26     m_process_sp (),
27     m_thread_sp (),
28     m_frame_sp ()
29 {
30 }
31
32 ExecutionContext::ExecutionContext (const ExecutionContext &rhs) :
33     m_target_sp(rhs.m_target_sp),
34     m_process_sp(rhs.m_process_sp),
35     m_thread_sp(rhs.m_thread_sp),
36     m_frame_sp(rhs.m_frame_sp)
37 {
38 }
39
40 ExecutionContext::ExecutionContext (const lldb::TargetSP &target_sp, bool get_process) :
41     m_target_sp (),
42     m_process_sp (),
43     m_thread_sp (),
44     m_frame_sp ()
45 {
46     if (target_sp)
47         SetContext (target_sp, get_process);
48 }
49
50 ExecutionContext::ExecutionContext (const lldb::ProcessSP &process_sp) :
51     m_target_sp (),
52     m_process_sp (),
53     m_thread_sp (),
54     m_frame_sp ()
55 {
56     if (process_sp)
57         SetContext (process_sp);
58 }
59
60 ExecutionContext::ExecutionContext (const lldb::ThreadSP &thread_sp) :
61     m_target_sp (),
62     m_process_sp (),
63     m_thread_sp (),
64     m_frame_sp ()
65 {
66     if (thread_sp)
67         SetContext (thread_sp);
68 }
69
70 ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
71     m_target_sp (),
72     m_process_sp (),
73     m_thread_sp (),
74     m_frame_sp ()
75 {
76     if (frame_sp)
77         SetContext (frame_sp);
78 }
79
80 ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
81     m_target_sp (),
82     m_process_sp (),
83     m_thread_sp (),
84     m_frame_sp ()
85 {
86     lldb::TargetSP target_sp(target_wp.lock());
87     if (target_sp)
88         SetContext (target_sp, get_process);
89 }
90
91 ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
92     m_target_sp (),
93     m_process_sp (),
94     m_thread_sp (),
95     m_frame_sp ()
96 {
97     lldb::ProcessSP process_sp(process_wp.lock());
98     if (process_sp)
99         SetContext (process_sp);
100 }
101
102 ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
103     m_target_sp (),
104     m_process_sp (),
105     m_thread_sp (),
106     m_frame_sp ()
107 {
108     lldb::ThreadSP thread_sp(thread_wp.lock());
109     if (thread_sp)
110         SetContext (thread_sp);
111 }
112
113 ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
114     m_target_sp (),
115     m_process_sp (),
116     m_thread_sp (),
117     m_frame_sp ()
118 {
119     lldb::StackFrameSP frame_sp(frame_wp.lock());
120     if (frame_sp)
121         SetContext (frame_sp);
122 }
123
124 ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
125     m_target_sp (),
126     m_process_sp (),
127     m_thread_sp (),
128     m_frame_sp ()
129 {
130     if (t)
131     {
132         m_target_sp = t->shared_from_this();
133         if (fill_current_process_thread_frame)
134         {
135             m_process_sp = t->GetProcessSP();
136             if (m_process_sp)
137             {
138                 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
139                 if (m_thread_sp)
140                     m_frame_sp = m_thread_sp->GetSelectedFrame();
141             }
142         }
143     }
144 }
145
146 ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
147     m_target_sp (),
148     m_process_sp (),
149     m_thread_sp (),
150     m_frame_sp ()
151 {
152     if (process)
153     {
154         m_process_sp = process->shared_from_this();
155         m_target_sp = process->GetTarget().shared_from_this();
156     }
157     if (thread)
158         m_thread_sp = thread->shared_from_this();
159     if (frame)
160         m_frame_sp = frame->shared_from_this();
161 }
162
163 ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref) :
164     m_target_sp (exe_ctx_ref.GetTargetSP()),
165     m_process_sp (exe_ctx_ref.GetProcessSP()),
166     m_thread_sp (exe_ctx_ref.GetThreadSP()),
167     m_frame_sp (exe_ctx_ref.GetFrameSP())
168 {
169 }
170
171 ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, bool thread_and_frame_only_if_stopped) :
172     m_target_sp (),
173     m_process_sp (),
174     m_thread_sp (),
175     m_frame_sp ()
176 {
177     if (exe_ctx_ref_ptr)
178     {
179         m_target_sp  = exe_ctx_ref_ptr->GetTargetSP();
180         m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
181         if (!thread_and_frame_only_if_stopped || (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true)))
182         {
183             m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
184             m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
185         }
186     }
187 }
188
189 ExecutionContext::ExecutionContext(const ExecutionContextRef *exe_ctx_ref_ptr,
190                                    std::unique_lock<std::recursive_mutex> &lock)
191     : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp()
192 {
193     if (exe_ctx_ref_ptr)
194     {
195         m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
196         if (m_target_sp)
197         {
198             lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
199
200             m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
201             m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
202             m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
203         }
204     }
205 }
206
207 ExecutionContext::ExecutionContext(const ExecutionContextRef &exe_ctx_ref, std::unique_lock<std::recursive_mutex> &lock)
208     : m_target_sp(exe_ctx_ref.GetTargetSP()), m_process_sp(), m_thread_sp(), m_frame_sp()
209 {
210     if (m_target_sp)
211     {
212         lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
213
214         m_process_sp = exe_ctx_ref.GetProcessSP();
215         m_thread_sp = exe_ctx_ref.GetThreadSP();
216         m_frame_sp = exe_ctx_ref.GetFrameSP();
217     }
218 }
219
220 ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
221     m_target_sp (),
222     m_process_sp (),
223     m_thread_sp (),
224     m_frame_sp ()
225 {
226     if (exe_scope_ptr)
227         exe_scope_ptr->CalculateExecutionContext (*this);
228 }
229
230 ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
231 {
232     exe_scope_ref.CalculateExecutionContext (*this);
233 }
234
235 void
236 ExecutionContext::Clear()
237 {
238     m_target_sp.reset();
239     m_process_sp.reset();
240     m_thread_sp.reset();
241     m_frame_sp.reset();
242 }
243
244 ExecutionContext::~ExecutionContext() = default;
245
246 uint32_t
247 ExecutionContext::GetAddressByteSize() const
248 {
249     if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
250         return m_target_sp->GetArchitecture().GetAddressByteSize();
251     if (m_process_sp)
252         return m_process_sp->GetAddressByteSize();
253     return sizeof(void *);
254 }
255
256 lldb::ByteOrder
257 ExecutionContext::GetByteOrder() const
258 {
259     if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
260         m_target_sp->GetArchitecture().GetByteOrder();
261     if (m_process_sp)
262         m_process_sp->GetByteOrder();
263     return endian::InlHostByteOrder();
264 }
265
266 RegisterContext *
267 ExecutionContext::GetRegisterContext () const
268 {
269     if (m_frame_sp)
270         return m_frame_sp->GetRegisterContext().get();
271     else if (m_thread_sp)
272         return m_thread_sp->GetRegisterContext().get();
273     return nullptr;
274 }
275
276 Target *
277 ExecutionContext::GetTargetPtr () const
278 {
279     if (m_target_sp)
280         return m_target_sp.get();
281     if (m_process_sp)
282         return &m_process_sp->GetTarget();
283     return nullptr;
284 }
285
286 Process *
287 ExecutionContext::GetProcessPtr () const
288 {
289     if (m_process_sp)
290         return m_process_sp.get();
291     if (m_target_sp)
292         return m_target_sp->GetProcessSP().get();
293     return nullptr;
294 }
295
296 ExecutionContextScope *
297 ExecutionContext::GetBestExecutionContextScope () const
298 {
299     if (m_frame_sp)
300         return m_frame_sp.get();
301     if (m_thread_sp)
302         return m_thread_sp.get();
303     if (m_process_sp)
304         return m_process_sp.get();
305     return m_target_sp.get();
306 }
307
308 Target &
309 ExecutionContext::GetTargetRef () const
310 {
311 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
312     assert (m_target_sp);
313 #endif
314     return *m_target_sp;
315 }
316
317 Process &
318 ExecutionContext::GetProcessRef () const
319 {
320 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
321     assert (m_process_sp);
322 #endif
323     return *m_process_sp;
324 }
325
326 Thread &
327 ExecutionContext::GetThreadRef () const
328 {
329 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
330     assert (m_thread_sp);
331 #endif
332     return *m_thread_sp;
333 }
334
335 StackFrame &
336 ExecutionContext::GetFrameRef () const
337 {
338 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
339     assert (m_frame_sp);
340 #endif
341     return *m_frame_sp;
342 }
343
344 void
345 ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
346 {
347     m_target_sp = target_sp;
348 }
349
350 void
351 ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
352 {
353     m_process_sp = process_sp;
354 }
355
356 void
357 ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
358 {
359     m_thread_sp = thread_sp;
360 }
361
362 void
363 ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
364 {
365     m_frame_sp = frame_sp;
366 }
367
368 void
369 ExecutionContext::SetTargetPtr (Target* target)
370 {
371     if (target)
372         m_target_sp = target->shared_from_this();
373     else
374         m_target_sp.reset();
375 }
376
377 void
378 ExecutionContext::SetProcessPtr (Process *process)
379 {
380     if (process)
381         m_process_sp = process->shared_from_this();
382     else
383         m_process_sp.reset();
384 }
385
386 void
387 ExecutionContext::SetThreadPtr (Thread *thread)
388 {
389     if (thread)
390         m_thread_sp = thread->shared_from_this();
391     else
392         m_thread_sp.reset();
393 }
394
395 void
396 ExecutionContext::SetFramePtr (StackFrame *frame)
397 {
398     if (frame)
399         m_frame_sp = frame->shared_from_this();
400     else
401         m_frame_sp.reset();
402 }
403
404 void
405 ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
406 {
407     m_target_sp = target_sp;
408     if (get_process && target_sp)
409         m_process_sp = target_sp->GetProcessSP();
410     else
411         m_process_sp.reset();
412     m_thread_sp.reset();
413     m_frame_sp.reset();
414 }
415
416 void
417 ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
418 {
419     m_process_sp = process_sp;
420     if (process_sp)
421         m_target_sp = process_sp->GetTarget().shared_from_this();
422     else
423         m_target_sp.reset();
424     m_thread_sp.reset();
425     m_frame_sp.reset();
426 }
427
428 void
429 ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
430 {
431     m_frame_sp.reset();
432     m_thread_sp = thread_sp;
433     if (thread_sp)
434     {
435         m_process_sp = thread_sp->GetProcess();
436         if (m_process_sp)
437             m_target_sp = m_process_sp->GetTarget().shared_from_this();
438         else
439             m_target_sp.reset();
440     }
441     else
442     {
443         m_target_sp.reset();
444         m_process_sp.reset();
445     }
446 }
447
448 void
449 ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
450 {
451     m_frame_sp = frame_sp;
452     if (frame_sp)
453     {
454         m_thread_sp = frame_sp->CalculateThread();
455         if (m_thread_sp)
456         {
457             m_process_sp = m_thread_sp->GetProcess();
458             if (m_process_sp)
459                 m_target_sp = m_process_sp->GetTarget().shared_from_this();
460             else
461                 m_target_sp.reset();
462         }
463         else
464         {
465             m_target_sp.reset();
466             m_process_sp.reset();
467         }
468     }
469     else
470     {
471         m_target_sp.reset();
472         m_process_sp.reset();
473         m_thread_sp.reset();
474     }
475 }
476
477 ExecutionContext &
478 ExecutionContext::operator =(const ExecutionContext &rhs)
479 {
480     if (this != &rhs)
481     {
482         m_target_sp  = rhs.m_target_sp;
483         m_process_sp = rhs.m_process_sp;
484         m_thread_sp  = rhs.m_thread_sp;
485         m_frame_sp   = rhs.m_frame_sp;
486     }
487     return *this;
488 }
489
490 bool
491 ExecutionContext::operator ==(const ExecutionContext &rhs) const
492 {
493     // Check that the frame shared pointers match, or both are valid and their stack
494     // IDs match since sometimes we get new objects that represent the same 
495     // frame within a thread.
496     if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
497     {
498         // Check that the thread shared pointers match, or both are valid and 
499         // their thread IDs match since sometimes we get new objects that 
500         // represent the same thread within a process.
501         if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
502         {
503             // Processes and targets don't change much
504             return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
505         }
506     }
507     return false;
508 }
509
510 bool
511 ExecutionContext::operator !=(const ExecutionContext &rhs) const
512 {
513     return !(*this == rhs);
514 }
515
516 bool
517 ExecutionContext::HasTargetScope () const
518 {
519     return ((bool) m_target_sp
520             && m_target_sp->IsValid());
521 }
522
523 bool
524 ExecutionContext::HasProcessScope () const
525 {
526     return (HasTargetScope()
527             && ((bool) m_process_sp && m_process_sp->IsValid()));
528 }
529
530 bool
531 ExecutionContext::HasThreadScope () const
532 {
533     return (HasProcessScope()
534            && ((bool) m_thread_sp && m_thread_sp->IsValid()));
535 }
536
537 bool
538 ExecutionContext::HasFrameScope () const
539 {
540     return HasThreadScope() && m_frame_sp;
541 }
542
543 ExecutionContextRef::ExecutionContextRef() :
544     m_target_wp (),
545     m_process_wp (),
546     m_thread_wp (),
547     m_tid(LLDB_INVALID_THREAD_ID),
548     m_stack_id ()
549 {
550 }
551
552 ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
553     m_target_wp (),
554     m_process_wp (),
555     m_thread_wp (),
556     m_tid(LLDB_INVALID_THREAD_ID),
557     m_stack_id ()
558 {
559     if (exe_ctx)
560         *this = *exe_ctx;
561 }
562
563 ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
564     m_target_wp (),
565     m_process_wp (),
566     m_thread_wp (),
567     m_tid(LLDB_INVALID_THREAD_ID),
568     m_stack_id ()
569 {
570     *this = exe_ctx;
571 }
572
573 ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
574     m_target_wp(),
575     m_process_wp(),
576     m_thread_wp(),
577     m_tid(LLDB_INVALID_THREAD_ID),
578     m_stack_id ()
579 {
580     SetTargetPtr (target, adopt_selected);
581 }
582
583 ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
584     m_target_wp (rhs.m_target_wp),
585     m_process_wp(rhs.m_process_wp),
586     m_thread_wp (rhs.m_thread_wp),
587     m_tid       (rhs.m_tid),
588     m_stack_id  (rhs.m_stack_id)
589 {
590 }
591
592 ExecutionContextRef &
593 ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
594 {
595     if (this != &rhs)
596     {
597         m_target_wp  = rhs.m_target_wp;
598         m_process_wp = rhs.m_process_wp;
599         m_thread_wp  = rhs.m_thread_wp;
600         m_tid        = rhs.m_tid;
601         m_stack_id   = rhs.m_stack_id;
602     }
603     return *this;
604 }
605
606 ExecutionContextRef &
607 ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
608 {
609     m_target_wp = exe_ctx.GetTargetSP();
610     m_process_wp = exe_ctx.GetProcessSP();
611     lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
612     m_thread_wp = thread_sp;
613     if (thread_sp)
614         m_tid = thread_sp->GetID();
615     else
616         m_tid = LLDB_INVALID_THREAD_ID;
617     lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
618     if (frame_sp)
619         m_stack_id = frame_sp->GetStackID();
620     else
621         m_stack_id.Clear();
622     return *this;
623 }
624
625 void
626 ExecutionContextRef::Clear()
627 {
628     m_target_wp.reset();
629     m_process_wp.reset();
630     ClearThread();
631     ClearFrame();
632 }
633
634 ExecutionContextRef::~ExecutionContextRef() = default;
635
636 void
637 ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
638 {
639     m_target_wp = target_sp;
640 }
641
642 void
643 ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
644 {
645     if (process_sp)
646     {
647         m_process_wp = process_sp;
648         SetTargetSP (process_sp->GetTarget().shared_from_this());
649     }
650     else
651     {
652         m_process_wp.reset();
653         m_target_wp.reset();
654     }
655 }
656
657 void
658 ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
659 {
660     if (thread_sp)
661     {
662         m_thread_wp = thread_sp;
663         m_tid = thread_sp->GetID();
664         SetProcessSP (thread_sp->GetProcess());
665     }
666     else
667     {
668         ClearThread();
669         m_process_wp.reset();
670         m_target_wp.reset();
671     }
672 }
673
674 void
675 ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
676 {
677     if (frame_sp)
678     {
679         m_stack_id = frame_sp->GetStackID();
680         SetThreadSP (frame_sp->GetThread());
681     }
682     else
683     {
684         ClearFrame();
685         ClearThread();
686         m_process_wp.reset();
687         m_target_wp.reset();
688     }
689 }
690
691 void
692 ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
693 {
694     Clear();
695     if (target)
696     {
697         lldb::TargetSP target_sp (target->shared_from_this());
698         if (target_sp)
699         {
700             m_target_wp = target_sp;
701             if (adopt_selected)
702             {
703                 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
704                 if (process_sp)
705                 {
706                     m_process_wp = process_sp;
707                     if (process_sp)
708                     {
709                         // Only fill in the thread and frame if our process is stopped
710                         // Don't just check the state, since we might be in the middle of
711                         // resuming.
712                         Process::StopLocker stop_locker;
713
714                         if (stop_locker.TryLock(&process_sp->GetRunLock()) && StateIsStoppedState (process_sp->GetState(), true))
715                         {
716                             lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
717                             if (!thread_sp)
718                                 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
719                             
720                             if (thread_sp)
721                             {
722                                 SetThreadSP (thread_sp);
723                                 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
724                                 if (!frame_sp)
725                                     frame_sp = thread_sp->GetStackFrameAtIndex(0);
726                                 if (frame_sp)
727                                     SetFrameSP (frame_sp);
728                             }
729                         }
730                     }
731                 }
732             }
733         }
734     }
735 }
736
737 void
738 ExecutionContextRef::SetProcessPtr (Process *process)
739 {
740     if (process)
741     {
742         SetProcessSP(process->shared_from_this());
743     }
744     else
745     {
746         m_process_wp.reset();
747         m_target_wp.reset();
748     }
749 }
750
751 void
752 ExecutionContextRef::SetThreadPtr (Thread *thread)
753 {
754     if (thread)
755     {
756         SetThreadSP (thread->shared_from_this());
757     }
758     else
759     {
760         ClearThread();
761         m_process_wp.reset();
762         m_target_wp.reset();
763     }
764 }
765
766 void
767 ExecutionContextRef::SetFramePtr (StackFrame *frame)
768 {
769     if (frame)
770         SetFrameSP (frame->shared_from_this());
771     else
772         Clear();
773 }
774
775 lldb::TargetSP
776 ExecutionContextRef::GetTargetSP () const
777 {
778     lldb::TargetSP target_sp(m_target_wp.lock());
779     if (target_sp && !target_sp->IsValid())
780         target_sp.reset();
781     return target_sp;
782 }
783
784 lldb::ProcessSP
785 ExecutionContextRef::GetProcessSP () const
786 {
787     lldb::ProcessSP process_sp(m_process_wp.lock());
788     if (process_sp && !process_sp->IsValid())
789         process_sp.reset();
790     return process_sp;
791 }
792
793 lldb::ThreadSP
794 ExecutionContextRef::GetThreadSP () const
795 {
796     lldb::ThreadSP thread_sp (m_thread_wp.lock());
797     
798     if (m_tid != LLDB_INVALID_THREAD_ID)
799     {
800         // We check if the thread has been destroyed in cases where clients
801         // might still have shared pointer to a thread, but the thread is
802         // not valid anymore (not part of the process)
803         if (!thread_sp || !thread_sp->IsValid())
804         {
805             lldb::ProcessSP process_sp(GetProcessSP());
806             if (process_sp && process_sp->IsValid())
807             {
808                 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
809                 m_thread_wp = thread_sp;
810             }
811         }
812     }
813     
814     // Check that we aren't about to return an invalid thread sp.  We might return a nullptr thread_sp,
815     // but don't return an invalid one.
816     
817     if (thread_sp && !thread_sp->IsValid())
818         thread_sp.reset();
819     
820     return thread_sp;
821 }
822
823 lldb::StackFrameSP
824 ExecutionContextRef::GetFrameSP () const
825 {
826     if (m_stack_id.IsValid())
827     {
828         lldb::ThreadSP thread_sp (GetThreadSP());
829         if (thread_sp)
830             return thread_sp->GetFrameWithStackID (m_stack_id);
831     }
832     return lldb::StackFrameSP();
833 }
834
835 ExecutionContext
836 ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
837 {
838     return ExecutionContext(this, thread_and_frame_only_if_stopped);
839 }