1 //===-- ExecutionContext.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework 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"
22 using namespace lldb_private;
24 ExecutionContext::ExecutionContext() :
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)
40 ExecutionContext::ExecutionContext (const lldb::TargetSP &target_sp, bool get_process) :
47 SetContext (target_sp, get_process);
50 ExecutionContext::ExecutionContext (const lldb::ProcessSP &process_sp) :
57 SetContext (process_sp);
60 ExecutionContext::ExecutionContext (const lldb::ThreadSP &thread_sp) :
67 SetContext (thread_sp);
70 ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
77 SetContext (frame_sp);
80 ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
86 lldb::TargetSP target_sp(target_wp.lock());
88 SetContext (target_sp, get_process);
91 ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
97 lldb::ProcessSP process_sp(process_wp.lock());
99 SetContext (process_sp);
102 ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
108 lldb::ThreadSP thread_sp(thread_wp.lock());
110 SetContext (thread_sp);
113 ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
119 lldb::StackFrameSP frame_sp(frame_wp.lock());
121 SetContext (frame_sp);
124 ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
132 m_target_sp = t->shared_from_this();
133 if (fill_current_process_thread_frame)
135 m_process_sp = t->GetProcessSP();
138 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
140 m_frame_sp = m_thread_sp->GetSelectedFrame();
146 ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
154 m_process_sp = process->shared_from_this();
155 m_target_sp = process->GetTarget().shared_from_this();
158 m_thread_sp = thread->shared_from_this();
160 m_frame_sp = frame->shared_from_this();
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())
171 ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, bool thread_and_frame_only_if_stopped) :
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)))
183 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
184 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
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()
195 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
198 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
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();
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()
212 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
214 m_process_sp = exe_ctx_ref.GetProcessSP();
215 m_thread_sp = exe_ctx_ref.GetThreadSP();
216 m_frame_sp = exe_ctx_ref.GetFrameSP();
220 ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
227 exe_scope_ptr->CalculateExecutionContext (*this);
230 ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
232 exe_scope_ref.CalculateExecutionContext (*this);
236 ExecutionContext::Clear()
239 m_process_sp.reset();
244 ExecutionContext::~ExecutionContext() = default;
247 ExecutionContext::GetAddressByteSize() const
249 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
250 return m_target_sp->GetArchitecture().GetAddressByteSize();
252 return m_process_sp->GetAddressByteSize();
253 return sizeof(void *);
257 ExecutionContext::GetByteOrder() const
259 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
260 m_target_sp->GetArchitecture().GetByteOrder();
262 m_process_sp->GetByteOrder();
263 return endian::InlHostByteOrder();
267 ExecutionContext::GetRegisterContext () const
270 return m_frame_sp->GetRegisterContext().get();
271 else if (m_thread_sp)
272 return m_thread_sp->GetRegisterContext().get();
277 ExecutionContext::GetTargetPtr () const
280 return m_target_sp.get();
282 return &m_process_sp->GetTarget();
287 ExecutionContext::GetProcessPtr () const
290 return m_process_sp.get();
292 return m_target_sp->GetProcessSP().get();
296 ExecutionContextScope *
297 ExecutionContext::GetBestExecutionContextScope () const
300 return m_frame_sp.get();
302 return m_thread_sp.get();
304 return m_process_sp.get();
305 return m_target_sp.get();
309 ExecutionContext::GetTargetRef () const
311 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
312 assert (m_target_sp);
318 ExecutionContext::GetProcessRef () const
320 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
321 assert (m_process_sp);
323 return *m_process_sp;
327 ExecutionContext::GetThreadRef () const
329 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
330 assert (m_thread_sp);
336 ExecutionContext::GetFrameRef () const
338 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
345 ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
347 m_target_sp = target_sp;
351 ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
353 m_process_sp = process_sp;
357 ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
359 m_thread_sp = thread_sp;
363 ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
365 m_frame_sp = frame_sp;
369 ExecutionContext::SetTargetPtr (Target* target)
372 m_target_sp = target->shared_from_this();
378 ExecutionContext::SetProcessPtr (Process *process)
381 m_process_sp = process->shared_from_this();
383 m_process_sp.reset();
387 ExecutionContext::SetThreadPtr (Thread *thread)
390 m_thread_sp = thread->shared_from_this();
396 ExecutionContext::SetFramePtr (StackFrame *frame)
399 m_frame_sp = frame->shared_from_this();
405 ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
407 m_target_sp = target_sp;
408 if (get_process && target_sp)
409 m_process_sp = target_sp->GetProcessSP();
411 m_process_sp.reset();
417 ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
419 m_process_sp = process_sp;
421 m_target_sp = process_sp->GetTarget().shared_from_this();
429 ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
432 m_thread_sp = thread_sp;
435 m_process_sp = thread_sp->GetProcess();
437 m_target_sp = m_process_sp->GetTarget().shared_from_this();
444 m_process_sp.reset();
449 ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
451 m_frame_sp = frame_sp;
454 m_thread_sp = frame_sp->CalculateThread();
457 m_process_sp = m_thread_sp->GetProcess();
459 m_target_sp = m_process_sp->GetTarget().shared_from_this();
466 m_process_sp.reset();
472 m_process_sp.reset();
478 ExecutionContext::operator =(const ExecutionContext &rhs)
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;
491 ExecutionContext::operator ==(const ExecutionContext &rhs) const
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()))
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()))
503 // Processes and targets don't change much
504 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
511 ExecutionContext::operator !=(const ExecutionContext &rhs) const
513 return !(*this == rhs);
517 ExecutionContext::HasTargetScope () const
519 return ((bool) m_target_sp
520 && m_target_sp->IsValid());
524 ExecutionContext::HasProcessScope () const
526 return (HasTargetScope()
527 && ((bool) m_process_sp && m_process_sp->IsValid()));
531 ExecutionContext::HasThreadScope () const
533 return (HasProcessScope()
534 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
538 ExecutionContext::HasFrameScope () const
540 return HasThreadScope() && m_frame_sp;
543 ExecutionContextRef::ExecutionContextRef() :
547 m_tid(LLDB_INVALID_THREAD_ID),
552 ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
556 m_tid(LLDB_INVALID_THREAD_ID),
563 ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
567 m_tid(LLDB_INVALID_THREAD_ID),
573 ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
577 m_tid(LLDB_INVALID_THREAD_ID),
580 SetTargetPtr (target, adopt_selected);
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),
588 m_stack_id (rhs.m_stack_id)
592 ExecutionContextRef &
593 ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
597 m_target_wp = rhs.m_target_wp;
598 m_process_wp = rhs.m_process_wp;
599 m_thread_wp = rhs.m_thread_wp;
601 m_stack_id = rhs.m_stack_id;
606 ExecutionContextRef &
607 ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
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;
614 m_tid = thread_sp->GetID();
616 m_tid = LLDB_INVALID_THREAD_ID;
617 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
619 m_stack_id = frame_sp->GetStackID();
626 ExecutionContextRef::Clear()
629 m_process_wp.reset();
634 ExecutionContextRef::~ExecutionContextRef() = default;
637 ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
639 m_target_wp = target_sp;
643 ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
647 m_process_wp = process_sp;
648 SetTargetSP (process_sp->GetTarget().shared_from_this());
652 m_process_wp.reset();
658 ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
662 m_thread_wp = thread_sp;
663 m_tid = thread_sp->GetID();
664 SetProcessSP (thread_sp->GetProcess());
669 m_process_wp.reset();
675 ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
679 m_stack_id = frame_sp->GetStackID();
680 SetThreadSP (frame_sp->GetThread());
686 m_process_wp.reset();
692 ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
697 lldb::TargetSP target_sp (target->shared_from_this());
700 m_target_wp = target_sp;
703 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
706 m_process_wp = process_sp;
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
712 Process::StopLocker stop_locker;
714 if (stop_locker.TryLock(&process_sp->GetRunLock()) && StateIsStoppedState (process_sp->GetState(), true))
716 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
718 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
722 SetThreadSP (thread_sp);
723 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
725 frame_sp = thread_sp->GetStackFrameAtIndex(0);
727 SetFrameSP (frame_sp);
738 ExecutionContextRef::SetProcessPtr (Process *process)
742 SetProcessSP(process->shared_from_this());
746 m_process_wp.reset();
752 ExecutionContextRef::SetThreadPtr (Thread *thread)
756 SetThreadSP (thread->shared_from_this());
761 m_process_wp.reset();
767 ExecutionContextRef::SetFramePtr (StackFrame *frame)
770 SetFrameSP (frame->shared_from_this());
776 ExecutionContextRef::GetTargetSP () const
778 lldb::TargetSP target_sp(m_target_wp.lock());
779 if (target_sp && !target_sp->IsValid())
785 ExecutionContextRef::GetProcessSP () const
787 lldb::ProcessSP process_sp(m_process_wp.lock());
788 if (process_sp && !process_sp->IsValid())
794 ExecutionContextRef::GetThreadSP () const
796 lldb::ThreadSP thread_sp (m_thread_wp.lock());
798 if (m_tid != LLDB_INVALID_THREAD_ID)
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())
805 lldb::ProcessSP process_sp(GetProcessSP());
806 if (process_sp && process_sp->IsValid())
808 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
809 m_thread_wp = thread_sp;
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.
817 if (thread_sp && !thread_sp->IsValid())
824 ExecutionContextRef::GetFrameSP () const
826 if (m_stack_id.IsValid())
828 lldb::ThreadSP thread_sp (GetThreadSP());
830 return thread_sp->GetFrameWithStackID (m_stack_id);
832 return lldb::StackFrameSP();
836 ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
838 return ExecutionContext(this, thread_and_frame_only_if_stopped);