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) :
122 m_target_sp (t->shared_from_this()),
127 if (t && fill_current_process_thread_frame)
129 m_process_sp = t->GetProcessSP();
132 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
134 m_frame_sp = m_thread_sp->GetSelectedFrame();
139 ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
141 m_process_sp (process->shared_from_this()),
142 m_thread_sp (thread->shared_from_this()),
143 m_frame_sp (frame->shared_from_this())
146 m_target_sp = process->GetTarget().shared_from_this();
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())
157 ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr) :
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();
172 ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
180 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
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();
191 ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
192 m_target_sp (exe_ctx_ref.GetTargetSP()),
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();
206 ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
213 exe_scope_ptr->CalculateExecutionContext (*this);
216 ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
218 exe_scope_ref.CalculateExecutionContext (*this);
222 ExecutionContext::Clear()
225 m_process_sp.reset();
230 ExecutionContext::~ExecutionContext()
235 ExecutionContext::GetAddressByteSize() const
237 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
238 m_target_sp->GetArchitecture().GetAddressByteSize();
240 m_process_sp->GetAddressByteSize();
241 return sizeof(void *);
247 ExecutionContext::GetRegisterContext () const
250 return m_frame_sp->GetRegisterContext().get();
251 else if (m_thread_sp)
252 return m_thread_sp->GetRegisterContext().get();
257 ExecutionContext::GetTargetPtr () const
260 return m_target_sp.get();
262 return &m_process_sp->GetTarget();
267 ExecutionContext::GetProcessPtr () const
270 return m_process_sp.get();
272 return m_target_sp->GetProcessSP().get();
276 ExecutionContextScope *
277 ExecutionContext::GetBestExecutionContextScope () const
280 return m_frame_sp.get();
282 return m_thread_sp.get();
284 return m_process_sp.get();
285 return m_target_sp.get();
289 ExecutionContext::GetTargetRef () const
291 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
292 assert (m_target_sp.get());
298 ExecutionContext::GetProcessRef () const
300 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
301 assert (m_process_sp.get());
303 return *m_process_sp;
307 ExecutionContext::GetThreadRef () const
309 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
310 assert (m_thread_sp.get());
316 ExecutionContext::GetFrameRef () const
318 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
319 assert (m_frame_sp.get());
325 ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
327 m_target_sp = target_sp;
331 ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
333 m_process_sp = process_sp;
337 ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
339 m_thread_sp = thread_sp;
343 ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
345 m_frame_sp = frame_sp;
349 ExecutionContext::SetTargetPtr (Target* target)
352 m_target_sp = target->shared_from_this();
358 ExecutionContext::SetProcessPtr (Process *process)
361 m_process_sp = process->shared_from_this();
363 m_process_sp.reset();
367 ExecutionContext::SetThreadPtr (Thread *thread)
370 m_thread_sp = thread->shared_from_this();
376 ExecutionContext::SetFramePtr (StackFrame *frame)
379 m_frame_sp = frame->shared_from_this();
385 ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
387 m_target_sp = target_sp;
388 if (get_process && target_sp)
389 m_process_sp = target_sp->GetProcessSP();
391 m_process_sp.reset();
397 ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
399 m_process_sp = process_sp;
401 m_target_sp = process_sp->GetTarget().shared_from_this();
409 ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
412 m_thread_sp = thread_sp;
415 m_process_sp = thread_sp->GetProcess();
417 m_target_sp = m_process_sp->GetTarget().shared_from_this();
424 m_process_sp.reset();
429 ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
431 m_frame_sp = frame_sp;
434 m_thread_sp = frame_sp->CalculateThread();
437 m_process_sp = m_thread_sp->GetProcess();
439 m_target_sp = m_process_sp->GetTarget().shared_from_this();
446 m_process_sp.reset();
452 m_process_sp.reset();
458 ExecutionContext::operator =(const ExecutionContext &rhs)
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;
471 ExecutionContext::operator ==(const ExecutionContext &rhs) const
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()))
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()))
483 // Processes and targets don't change much
484 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
491 ExecutionContext::operator !=(const ExecutionContext &rhs) const
493 return !(*this == rhs);
497 ExecutionContext::HasTargetScope () const
499 return ((bool) m_target_sp
500 && m_target_sp->IsValid());
504 ExecutionContext::HasProcessScope () const
506 return (HasTargetScope()
507 && ((bool) m_process_sp && m_process_sp->IsValid()));
511 ExecutionContext::HasThreadScope () const
513 return (HasProcessScope()
514 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
518 ExecutionContext::HasFrameScope () const
520 return HasThreadScope() && m_frame_sp;
523 ExecutionContextRef::ExecutionContextRef() :
527 m_tid(LLDB_INVALID_THREAD_ID),
532 ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
536 m_tid(LLDB_INVALID_THREAD_ID),
543 ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
547 m_tid(LLDB_INVALID_THREAD_ID),
554 ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
558 m_tid(LLDB_INVALID_THREAD_ID),
561 SetTargetPtr (target, adopt_selected);
567 ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
568 m_target_wp (rhs.m_target_wp),
569 m_process_wp(rhs.m_process_wp),
570 m_thread_wp (rhs.m_thread_wp),
572 m_stack_id (rhs.m_stack_id)
576 ExecutionContextRef &
577 ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
581 m_target_wp = rhs.m_target_wp;
582 m_process_wp = rhs.m_process_wp;
583 m_thread_wp = rhs.m_thread_wp;
585 m_stack_id = rhs.m_stack_id;
590 ExecutionContextRef &
591 ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
593 m_target_wp = exe_ctx.GetTargetSP();
594 m_process_wp = exe_ctx.GetProcessSP();
595 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
596 m_thread_wp = thread_sp;
598 m_tid = thread_sp->GetID();
600 m_tid = LLDB_INVALID_THREAD_ID;
601 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
603 m_stack_id = frame_sp->GetStackID();
610 ExecutionContextRef::Clear()
613 m_process_wp.reset();
618 ExecutionContextRef::~ExecutionContextRef()
623 ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
625 m_target_wp = target_sp;
629 ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
633 m_process_wp = process_sp;
634 SetTargetSP (process_sp->GetTarget().shared_from_this());
638 m_process_wp.reset();
644 ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
648 m_thread_wp = thread_sp;
649 m_tid = thread_sp->GetID();
650 SetProcessSP (thread_sp->GetProcess());
655 m_process_wp.reset();
661 ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
665 m_stack_id = frame_sp->GetStackID();
666 SetThreadSP (frame_sp->GetThread());
672 m_process_wp.reset();
679 ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
684 lldb::TargetSP target_sp (target->shared_from_this());
687 m_target_wp = target_sp;
690 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
693 m_process_wp = process_sp;
696 // Only fill in the thread and frame if our process is stopped
697 if (StateIsStoppedState (process_sp->GetState(), true))
699 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
701 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
705 SetThreadSP (thread_sp);
706 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
708 frame_sp = thread_sp->GetStackFrameAtIndex(0);
710 SetFrameSP (frame_sp);
721 ExecutionContextRef::SetProcessPtr (Process *process)
725 SetProcessSP(process->shared_from_this());
729 m_process_wp.reset();
735 ExecutionContextRef::SetThreadPtr (Thread *thread)
739 SetThreadSP (thread->shared_from_this());
744 m_process_wp.reset();
750 ExecutionContextRef::SetFramePtr (StackFrame *frame)
753 SetFrameSP (frame->shared_from_this());
759 ExecutionContextRef::GetTargetSP () const
761 lldb::TargetSP target_sp(m_target_wp.lock());
762 if (target_sp && !target_sp->IsValid())
768 ExecutionContextRef::GetProcessSP () const
770 lldb::ProcessSP process_sp(m_process_wp.lock());
771 if (process_sp && !process_sp->IsValid())
777 ExecutionContextRef::GetThreadSP () const
779 lldb::ThreadSP thread_sp (m_thread_wp.lock());
781 if (m_tid != LLDB_INVALID_THREAD_ID)
783 // We check if the thread has been destroyed in cases where clients
784 // might still have shared pointer to a thread, but the thread is
785 // not valid anymore (not part of the process)
786 if (!thread_sp || !thread_sp->IsValid())
788 lldb::ProcessSP process_sp(GetProcessSP());
789 if (process_sp && process_sp->IsValid())
791 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
792 m_thread_wp = thread_sp;
797 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
798 // but don't return an invalid one.
800 if (thread_sp && !thread_sp->IsValid())
807 ExecutionContextRef::GetFrameSP () const
809 if (m_stack_id.IsValid())
811 lldb::ThreadSP thread_sp (GetThreadSP());
813 return thread_sp->GetFrameWithStackID (m_stack_id);
815 return lldb::StackFrameSP();
819 ExecutionContextRef::Lock () const
821 return ExecutionContext(this);