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, bool thread_and_frame_only_if_stopped) :
165 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
166 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
167 if (!thread_and_frame_only_if_stopped || (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true)))
169 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
170 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
175 ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
183 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
186 locker.Lock(m_target_sp->GetAPIMutex());
187 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
188 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
189 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
194 ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
195 m_target_sp (exe_ctx_ref.GetTargetSP()),
202 locker.Lock(m_target_sp->GetAPIMutex());
203 m_process_sp = exe_ctx_ref.GetProcessSP();
204 m_thread_sp = exe_ctx_ref.GetThreadSP();
205 m_frame_sp = exe_ctx_ref.GetFrameSP();
209 ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
216 exe_scope_ptr->CalculateExecutionContext (*this);
219 ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
221 exe_scope_ref.CalculateExecutionContext (*this);
225 ExecutionContext::Clear()
228 m_process_sp.reset();
233 ExecutionContext::~ExecutionContext()
238 ExecutionContext::GetAddressByteSize() const
240 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
241 m_target_sp->GetArchitecture().GetAddressByteSize();
243 m_process_sp->GetAddressByteSize();
244 return sizeof(void *);
248 ExecutionContext::GetByteOrder() const
250 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
251 m_target_sp->GetArchitecture().GetByteOrder();
253 m_process_sp->GetByteOrder();
254 return lldb::endian::InlHostByteOrder();
258 ExecutionContext::GetRegisterContext () const
261 return m_frame_sp->GetRegisterContext().get();
262 else if (m_thread_sp)
263 return m_thread_sp->GetRegisterContext().get();
268 ExecutionContext::GetTargetPtr () const
271 return m_target_sp.get();
273 return &m_process_sp->GetTarget();
278 ExecutionContext::GetProcessPtr () const
281 return m_process_sp.get();
283 return m_target_sp->GetProcessSP().get();
287 ExecutionContextScope *
288 ExecutionContext::GetBestExecutionContextScope () const
291 return m_frame_sp.get();
293 return m_thread_sp.get();
295 return m_process_sp.get();
296 return m_target_sp.get();
300 ExecutionContext::GetTargetRef () const
302 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
303 assert (m_target_sp.get());
309 ExecutionContext::GetProcessRef () const
311 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
312 assert (m_process_sp.get());
314 return *m_process_sp;
318 ExecutionContext::GetThreadRef () const
320 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
321 assert (m_thread_sp.get());
327 ExecutionContext::GetFrameRef () const
329 #if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
330 assert (m_frame_sp.get());
336 ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
338 m_target_sp = target_sp;
342 ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
344 m_process_sp = process_sp;
348 ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
350 m_thread_sp = thread_sp;
354 ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
356 m_frame_sp = frame_sp;
360 ExecutionContext::SetTargetPtr (Target* target)
363 m_target_sp = target->shared_from_this();
369 ExecutionContext::SetProcessPtr (Process *process)
372 m_process_sp = process->shared_from_this();
374 m_process_sp.reset();
378 ExecutionContext::SetThreadPtr (Thread *thread)
381 m_thread_sp = thread->shared_from_this();
387 ExecutionContext::SetFramePtr (StackFrame *frame)
390 m_frame_sp = frame->shared_from_this();
396 ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
398 m_target_sp = target_sp;
399 if (get_process && target_sp)
400 m_process_sp = target_sp->GetProcessSP();
402 m_process_sp.reset();
408 ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
410 m_process_sp = process_sp;
412 m_target_sp = process_sp->GetTarget().shared_from_this();
420 ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
423 m_thread_sp = thread_sp;
426 m_process_sp = thread_sp->GetProcess();
428 m_target_sp = m_process_sp->GetTarget().shared_from_this();
435 m_process_sp.reset();
440 ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
442 m_frame_sp = frame_sp;
445 m_thread_sp = frame_sp->CalculateThread();
448 m_process_sp = m_thread_sp->GetProcess();
450 m_target_sp = m_process_sp->GetTarget().shared_from_this();
457 m_process_sp.reset();
463 m_process_sp.reset();
469 ExecutionContext::operator =(const ExecutionContext &rhs)
473 m_target_sp = rhs.m_target_sp;
474 m_process_sp = rhs.m_process_sp;
475 m_thread_sp = rhs.m_thread_sp;
476 m_frame_sp = rhs.m_frame_sp;
482 ExecutionContext::operator ==(const ExecutionContext &rhs) const
484 // Check that the frame shared pointers match, or both are valid and their stack
485 // IDs match since sometimes we get new objects that represent the same
486 // frame within a thread.
487 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
489 // Check that the thread shared pointers match, or both are valid and
490 // their thread IDs match since sometimes we get new objects that
491 // represent the same thread within a process.
492 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
494 // Processes and targets don't change much
495 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
502 ExecutionContext::operator !=(const ExecutionContext &rhs) const
504 return !(*this == rhs);
508 ExecutionContext::HasTargetScope () const
510 return ((bool) m_target_sp
511 && m_target_sp->IsValid());
515 ExecutionContext::HasProcessScope () const
517 return (HasTargetScope()
518 && ((bool) m_process_sp && m_process_sp->IsValid()));
522 ExecutionContext::HasThreadScope () const
524 return (HasProcessScope()
525 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
529 ExecutionContext::HasFrameScope () const
531 return HasThreadScope() && m_frame_sp;
534 ExecutionContextRef::ExecutionContextRef() :
538 m_tid(LLDB_INVALID_THREAD_ID),
543 ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
547 m_tid(LLDB_INVALID_THREAD_ID),
554 ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
558 m_tid(LLDB_INVALID_THREAD_ID),
565 ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
569 m_tid(LLDB_INVALID_THREAD_ID),
572 SetTargetPtr (target, adopt_selected);
578 ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
579 m_target_wp (rhs.m_target_wp),
580 m_process_wp(rhs.m_process_wp),
581 m_thread_wp (rhs.m_thread_wp),
583 m_stack_id (rhs.m_stack_id)
587 ExecutionContextRef &
588 ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
592 m_target_wp = rhs.m_target_wp;
593 m_process_wp = rhs.m_process_wp;
594 m_thread_wp = rhs.m_thread_wp;
596 m_stack_id = rhs.m_stack_id;
601 ExecutionContextRef &
602 ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
604 m_target_wp = exe_ctx.GetTargetSP();
605 m_process_wp = exe_ctx.GetProcessSP();
606 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
607 m_thread_wp = thread_sp;
609 m_tid = thread_sp->GetID();
611 m_tid = LLDB_INVALID_THREAD_ID;
612 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
614 m_stack_id = frame_sp->GetStackID();
621 ExecutionContextRef::Clear()
624 m_process_wp.reset();
629 ExecutionContextRef::~ExecutionContextRef()
634 ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
636 m_target_wp = target_sp;
640 ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
644 m_process_wp = process_sp;
645 SetTargetSP (process_sp->GetTarget().shared_from_this());
649 m_process_wp.reset();
655 ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
659 m_thread_wp = thread_sp;
660 m_tid = thread_sp->GetID();
661 SetProcessSP (thread_sp->GetProcess());
666 m_process_wp.reset();
672 ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
676 m_stack_id = frame_sp->GetStackID();
677 SetThreadSP (frame_sp->GetThread());
683 m_process_wp.reset();
690 ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
695 lldb::TargetSP target_sp (target->shared_from_this());
698 m_target_wp = target_sp;
701 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
704 m_process_wp = process_sp;
707 // Only fill in the thread and frame if our process is stopped
708 if (StateIsStoppedState (process_sp->GetState(), true))
710 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
712 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
716 SetThreadSP (thread_sp);
717 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
719 frame_sp = thread_sp->GetStackFrameAtIndex(0);
721 SetFrameSP (frame_sp);
732 ExecutionContextRef::SetProcessPtr (Process *process)
736 SetProcessSP(process->shared_from_this());
740 m_process_wp.reset();
746 ExecutionContextRef::SetThreadPtr (Thread *thread)
750 SetThreadSP (thread->shared_from_this());
755 m_process_wp.reset();
761 ExecutionContextRef::SetFramePtr (StackFrame *frame)
764 SetFrameSP (frame->shared_from_this());
770 ExecutionContextRef::GetTargetSP () const
772 lldb::TargetSP target_sp(m_target_wp.lock());
773 if (target_sp && !target_sp->IsValid())
779 ExecutionContextRef::GetProcessSP () const
781 lldb::ProcessSP process_sp(m_process_wp.lock());
782 if (process_sp && !process_sp->IsValid())
788 ExecutionContextRef::GetThreadSP () const
790 lldb::ThreadSP thread_sp (m_thread_wp.lock());
792 if (m_tid != LLDB_INVALID_THREAD_ID)
794 // We check if the thread has been destroyed in cases where clients
795 // might still have shared pointer to a thread, but the thread is
796 // not valid anymore (not part of the process)
797 if (!thread_sp || !thread_sp->IsValid())
799 lldb::ProcessSP process_sp(GetProcessSP());
800 if (process_sp && process_sp->IsValid())
802 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
803 m_thread_wp = thread_sp;
808 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
809 // but don't return an invalid one.
811 if (thread_sp && !thread_sp->IsValid())
818 ExecutionContextRef::GetFrameSP () const
820 if (m_stack_id.IsValid())
822 lldb::ThreadSP thread_sp (GetThreadSP());
824 return thread_sp->GetFrameWithStackID (m_stack_id);
826 return lldb::StackFrameSP();
830 ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
832 return ExecutionContext(this, thread_and_frame_only_if_stopped);