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 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ExecutionContext.h"
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"
19 using namespace lldb_private;
21 ExecutionContext::ExecutionContext() :
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)
37 ExecutionContext::ExecutionContext (const lldb::TargetSP &target_sp, bool get_process) :
44 SetContext (target_sp, get_process);
47 ExecutionContext::ExecutionContext (const lldb::ProcessSP &process_sp) :
54 SetContext (process_sp);
57 ExecutionContext::ExecutionContext (const lldb::ThreadSP &thread_sp) :
64 SetContext (thread_sp);
67 ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
74 SetContext (frame_sp);
77 ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
83 lldb::TargetSP target_sp(target_wp.lock());
85 SetContext (target_sp, get_process);
88 ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
94 lldb::ProcessSP process_sp(process_wp.lock());
96 SetContext (process_sp);
99 ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
105 lldb::ThreadSP thread_sp(thread_wp.lock());
107 SetContext (thread_sp);
110 ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
116 lldb::StackFrameSP frame_sp(frame_wp.lock());
118 SetContext (frame_sp);
121 ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
129 m_target_sp = t->shared_from_this();
130 if (fill_current_process_thread_frame)
132 m_process_sp = t->GetProcessSP();
135 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
137 m_frame_sp = m_thread_sp->GetSelectedFrame();
143 ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
151 m_process_sp = process->shared_from_this();
152 m_target_sp = process->GetTarget().shared_from_this();
155 m_thread_sp = thread->shared_from_this();
157 m_frame_sp = frame->shared_from_this();
160 ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref) :
161 m_target_sp (exe_ctx_ref.GetTargetSP()),
162 m_process_sp (exe_ctx_ref.GetProcessSP()),
163 m_thread_sp (exe_ctx_ref.GetThreadSP()),
164 m_frame_sp (exe_ctx_ref.GetFrameSP())
168 ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, bool thread_and_frame_only_if_stopped) :
176 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
177 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
178 if (!thread_and_frame_only_if_stopped || (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true)))
180 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
181 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
186 ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
194 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
197 locker.Lock(m_target_sp->GetAPIMutex());
198 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
199 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
200 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
205 ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
206 m_target_sp (exe_ctx_ref.GetTargetSP()),
213 locker.Lock(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()
249 ExecutionContext::GetAddressByteSize() const
251 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
252 return m_target_sp->GetArchitecture().GetAddressByteSize();
254 return m_process_sp->GetAddressByteSize();
255 return sizeof(void *);
259 ExecutionContext::GetByteOrder() const
261 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
262 m_target_sp->GetArchitecture().GetByteOrder();
264 m_process_sp->GetByteOrder();
265 return endian::InlHostByteOrder();
269 ExecutionContext::GetRegisterContext () const
272 return m_frame_sp->GetRegisterContext().get();
273 else if (m_thread_sp)
274 return m_thread_sp->GetRegisterContext().get();
279 ExecutionContext::GetTargetPtr () const
282 return m_target_sp.get();
284 return &m_process_sp->GetTarget();
289 ExecutionContext::GetProcessPtr () const
292 return m_process_sp.get();
294 return m_target_sp->GetProcessSP().get();
298 ExecutionContextScope *
299 ExecutionContext::GetBestExecutionContextScope () const
302 return m_frame_sp.get();
304 return m_thread_sp.get();
306 return m_process_sp.get();
307 return m_target_sp.get();
311 ExecutionContext::GetTargetRef () const
313 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
314 assert (m_target_sp.get());
320 ExecutionContext::GetProcessRef () const
322 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
323 assert (m_process_sp.get());
325 return *m_process_sp;
329 ExecutionContext::GetThreadRef () const
331 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
332 assert (m_thread_sp.get());
338 ExecutionContext::GetFrameRef () const
340 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
341 assert (m_frame_sp.get());
347 ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
349 m_target_sp = target_sp;
353 ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
355 m_process_sp = process_sp;
359 ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
361 m_thread_sp = thread_sp;
365 ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
367 m_frame_sp = frame_sp;
371 ExecutionContext::SetTargetPtr (Target* target)
374 m_target_sp = target->shared_from_this();
380 ExecutionContext::SetProcessPtr (Process *process)
383 m_process_sp = process->shared_from_this();
385 m_process_sp.reset();
389 ExecutionContext::SetThreadPtr (Thread *thread)
392 m_thread_sp = thread->shared_from_this();
398 ExecutionContext::SetFramePtr (StackFrame *frame)
401 m_frame_sp = frame->shared_from_this();
407 ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
409 m_target_sp = target_sp;
410 if (get_process && target_sp)
411 m_process_sp = target_sp->GetProcessSP();
413 m_process_sp.reset();
419 ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
421 m_process_sp = process_sp;
423 m_target_sp = process_sp->GetTarget().shared_from_this();
431 ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
434 m_thread_sp = thread_sp;
437 m_process_sp = thread_sp->GetProcess();
439 m_target_sp = m_process_sp->GetTarget().shared_from_this();
446 m_process_sp.reset();
451 ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
453 m_frame_sp = frame_sp;
456 m_thread_sp = frame_sp->CalculateThread();
459 m_process_sp = m_thread_sp->GetProcess();
461 m_target_sp = m_process_sp->GetTarget().shared_from_this();
468 m_process_sp.reset();
474 m_process_sp.reset();
480 ExecutionContext::operator =(const ExecutionContext &rhs)
484 m_target_sp = rhs.m_target_sp;
485 m_process_sp = rhs.m_process_sp;
486 m_thread_sp = rhs.m_thread_sp;
487 m_frame_sp = rhs.m_frame_sp;
493 ExecutionContext::operator ==(const ExecutionContext &rhs) const
495 // Check that the frame shared pointers match, or both are valid and their stack
496 // IDs match since sometimes we get new objects that represent the same
497 // frame within a thread.
498 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
500 // Check that the thread shared pointers match, or both are valid and
501 // their thread IDs match since sometimes we get new objects that
502 // represent the same thread within a process.
503 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
505 // Processes and targets don't change much
506 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
513 ExecutionContext::operator !=(const ExecutionContext &rhs) const
515 return !(*this == rhs);
519 ExecutionContext::HasTargetScope () const
521 return ((bool) m_target_sp
522 && m_target_sp->IsValid());
526 ExecutionContext::HasProcessScope () const
528 return (HasTargetScope()
529 && ((bool) m_process_sp && m_process_sp->IsValid()));
533 ExecutionContext::HasThreadScope () const
535 return (HasProcessScope()
536 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
540 ExecutionContext::HasFrameScope () const
542 return HasThreadScope() && m_frame_sp;
545 ExecutionContextRef::ExecutionContextRef() :
549 m_tid(LLDB_INVALID_THREAD_ID),
554 ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
558 m_tid(LLDB_INVALID_THREAD_ID),
565 ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
569 m_tid(LLDB_INVALID_THREAD_ID),
576 ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
580 m_tid(LLDB_INVALID_THREAD_ID),
583 SetTargetPtr (target, adopt_selected);
589 ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
590 m_target_wp (rhs.m_target_wp),
591 m_process_wp(rhs.m_process_wp),
592 m_thread_wp (rhs.m_thread_wp),
594 m_stack_id (rhs.m_stack_id)
598 ExecutionContextRef &
599 ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
603 m_target_wp = rhs.m_target_wp;
604 m_process_wp = rhs.m_process_wp;
605 m_thread_wp = rhs.m_thread_wp;
607 m_stack_id = rhs.m_stack_id;
612 ExecutionContextRef &
613 ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
615 m_target_wp = exe_ctx.GetTargetSP();
616 m_process_wp = exe_ctx.GetProcessSP();
617 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
618 m_thread_wp = thread_sp;
620 m_tid = thread_sp->GetID();
622 m_tid = LLDB_INVALID_THREAD_ID;
623 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
625 m_stack_id = frame_sp->GetStackID();
632 ExecutionContextRef::Clear()
635 m_process_wp.reset();
640 ExecutionContextRef::~ExecutionContextRef()
645 ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
647 m_target_wp = target_sp;
651 ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
655 m_process_wp = process_sp;
656 SetTargetSP (process_sp->GetTarget().shared_from_this());
660 m_process_wp.reset();
666 ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
670 m_thread_wp = thread_sp;
671 m_tid = thread_sp->GetID();
672 SetProcessSP (thread_sp->GetProcess());
677 m_process_wp.reset();
683 ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
687 m_stack_id = frame_sp->GetStackID();
688 SetThreadSP (frame_sp->GetThread());
694 m_process_wp.reset();
701 ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
706 lldb::TargetSP target_sp (target->shared_from_this());
709 m_target_wp = target_sp;
712 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
715 m_process_wp = process_sp;
718 // Only fill in the thread and frame if our process is stopped
719 // Don't just check the state, since we might be in the middle of
721 Process::StopLocker stop_locker;
723 if (stop_locker.TryLock(&process_sp->GetRunLock()) && StateIsStoppedState (process_sp->GetState(), true))
725 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
727 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
731 SetThreadSP (thread_sp);
732 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
734 frame_sp = thread_sp->GetStackFrameAtIndex(0);
736 SetFrameSP (frame_sp);
747 ExecutionContextRef::SetProcessPtr (Process *process)
751 SetProcessSP(process->shared_from_this());
755 m_process_wp.reset();
761 ExecutionContextRef::SetThreadPtr (Thread *thread)
765 SetThreadSP (thread->shared_from_this());
770 m_process_wp.reset();
776 ExecutionContextRef::SetFramePtr (StackFrame *frame)
779 SetFrameSP (frame->shared_from_this());
785 ExecutionContextRef::GetTargetSP () const
787 lldb::TargetSP target_sp(m_target_wp.lock());
788 if (target_sp && !target_sp->IsValid())
794 ExecutionContextRef::GetProcessSP () const
796 lldb::ProcessSP process_sp(m_process_wp.lock());
797 if (process_sp && !process_sp->IsValid())
803 ExecutionContextRef::GetThreadSP () const
805 lldb::ThreadSP thread_sp (m_thread_wp.lock());
807 if (m_tid != LLDB_INVALID_THREAD_ID)
809 // We check if the thread has been destroyed in cases where clients
810 // might still have shared pointer to a thread, but the thread is
811 // not valid anymore (not part of the process)
812 if (!thread_sp || !thread_sp->IsValid())
814 lldb::ProcessSP process_sp(GetProcessSP());
815 if (process_sp && process_sp->IsValid())
817 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
818 m_thread_wp = thread_sp;
823 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
824 // but don't return an invalid one.
826 if (thread_sp && !thread_sp->IsValid())
833 ExecutionContextRef::GetFrameSP () const
835 if (m_stack_id.IsValid())
837 lldb::ThreadSP thread_sp (GetThreadSP());
839 return thread_sp->GetFrameWithStackID (m_stack_id);
841 return lldb::StackFrameSP();
845 ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
847 return ExecutionContext(this, thread_and_frame_only_if_stopped);