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