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"
11 #include "lldb/Target/ExecutionContextScope.h"
12 #include "lldb/Target/Process.h"
13 #include "lldb/Target/StackFrame.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Utility/State.h"
18 using namespace lldb_private;
20 ExecutionContext::ExecutionContext()
21 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {}
23 ExecutionContext::ExecutionContext(const ExecutionContext &rhs)
24 : m_target_sp(rhs.m_target_sp), m_process_sp(rhs.m_process_sp),
25 m_thread_sp(rhs.m_thread_sp), m_frame_sp(rhs.m_frame_sp) {}
27 ExecutionContext::ExecutionContext(const lldb::TargetSP &target_sp,
29 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
31 SetContext(target_sp, get_process);
34 ExecutionContext::ExecutionContext(const lldb::ProcessSP &process_sp)
35 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
37 SetContext(process_sp);
40 ExecutionContext::ExecutionContext(const lldb::ThreadSP &thread_sp)
41 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
43 SetContext(thread_sp);
46 ExecutionContext::ExecutionContext(const lldb::StackFrameSP &frame_sp)
47 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
52 ExecutionContext::ExecutionContext(const lldb::TargetWP &target_wp,
54 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
55 lldb::TargetSP target_sp(target_wp.lock());
57 SetContext(target_sp, get_process);
60 ExecutionContext::ExecutionContext(const lldb::ProcessWP &process_wp)
61 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
62 lldb::ProcessSP process_sp(process_wp.lock());
64 SetContext(process_sp);
67 ExecutionContext::ExecutionContext(const lldb::ThreadWP &thread_wp)
68 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
69 lldb::ThreadSP thread_sp(thread_wp.lock());
71 SetContext(thread_sp);
74 ExecutionContext::ExecutionContext(const lldb::StackFrameWP &frame_wp)
75 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
76 lldb::StackFrameSP frame_sp(frame_wp.lock());
81 ExecutionContext::ExecutionContext(Target *t,
82 bool fill_current_process_thread_frame)
83 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
85 m_target_sp = t->shared_from_this();
86 if (fill_current_process_thread_frame) {
87 m_process_sp = t->GetProcessSP();
89 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
91 m_frame_sp = m_thread_sp->GetSelectedFrame();
97 ExecutionContext::ExecutionContext(Process *process, Thread *thread,
99 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
101 m_process_sp = process->shared_from_this();
102 m_target_sp = process->GetTarget().shared_from_this();
105 m_thread_sp = thread->shared_from_this();
107 m_frame_sp = frame->shared_from_this();
110 ExecutionContext::ExecutionContext(const ExecutionContextRef &exe_ctx_ref)
111 : m_target_sp(exe_ctx_ref.GetTargetSP()),
112 m_process_sp(exe_ctx_ref.GetProcessSP()),
113 m_thread_sp(exe_ctx_ref.GetThreadSP()),
114 m_frame_sp(exe_ctx_ref.GetFrameSP()) {}
116 ExecutionContext::ExecutionContext(const ExecutionContextRef *exe_ctx_ref_ptr,
117 bool thread_and_frame_only_if_stopped)
118 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
119 if (exe_ctx_ref_ptr) {
120 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
121 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
122 if (!thread_and_frame_only_if_stopped ||
123 (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true))) {
124 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
125 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
130 ExecutionContext::ExecutionContext(const ExecutionContextRef *exe_ctx_ref_ptr,
131 std::unique_lock<std::recursive_mutex> &lock)
132 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
133 if (exe_ctx_ref_ptr) {
134 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
136 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
138 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
139 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
140 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
145 ExecutionContext::ExecutionContext(const ExecutionContextRef &exe_ctx_ref,
146 std::unique_lock<std::recursive_mutex> &lock)
147 : m_target_sp(exe_ctx_ref.GetTargetSP()), m_process_sp(), m_thread_sp(),
150 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
152 m_process_sp = exe_ctx_ref.GetProcessSP();
153 m_thread_sp = exe_ctx_ref.GetThreadSP();
154 m_frame_sp = exe_ctx_ref.GetFrameSP();
158 ExecutionContext::ExecutionContext(ExecutionContextScope *exe_scope_ptr)
159 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
161 exe_scope_ptr->CalculateExecutionContext(*this);
164 ExecutionContext::ExecutionContext(ExecutionContextScope &exe_scope_ref) {
165 exe_scope_ref.CalculateExecutionContext(*this);
168 void ExecutionContext::Clear() {
170 m_process_sp.reset();
175 ExecutionContext::~ExecutionContext() = default;
177 uint32_t ExecutionContext::GetAddressByteSize() const {
178 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
179 return m_target_sp->GetArchitecture().GetAddressByteSize();
181 return m_process_sp->GetAddressByteSize();
182 return sizeof(void *);
185 lldb::ByteOrder ExecutionContext::GetByteOrder() const {
186 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
187 m_target_sp->GetArchitecture().GetByteOrder();
189 m_process_sp->GetByteOrder();
190 return endian::InlHostByteOrder();
193 RegisterContext *ExecutionContext::GetRegisterContext() const {
195 return m_frame_sp->GetRegisterContext().get();
196 else if (m_thread_sp)
197 return m_thread_sp->GetRegisterContext().get();
201 Target *ExecutionContext::GetTargetPtr() const {
203 return m_target_sp.get();
205 return &m_process_sp->GetTarget();
209 Process *ExecutionContext::GetProcessPtr() const {
211 return m_process_sp.get();
213 return m_target_sp->GetProcessSP().get();
217 ExecutionContextScope *ExecutionContext::GetBestExecutionContextScope() const {
219 return m_frame_sp.get();
221 return m_thread_sp.get();
223 return m_process_sp.get();
224 return m_target_sp.get();
227 Target &ExecutionContext::GetTargetRef() const {
228 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
234 Process &ExecutionContext::GetProcessRef() const {
235 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
236 assert(m_process_sp);
238 return *m_process_sp;
241 Thread &ExecutionContext::GetThreadRef() const {
242 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
248 StackFrame &ExecutionContext::GetFrameRef() const {
249 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
255 void ExecutionContext::SetTargetSP(const lldb::TargetSP &target_sp) {
256 m_target_sp = target_sp;
259 void ExecutionContext::SetProcessSP(const lldb::ProcessSP &process_sp) {
260 m_process_sp = process_sp;
263 void ExecutionContext::SetThreadSP(const lldb::ThreadSP &thread_sp) {
264 m_thread_sp = thread_sp;
267 void ExecutionContext::SetFrameSP(const lldb::StackFrameSP &frame_sp) {
268 m_frame_sp = frame_sp;
271 void ExecutionContext::SetTargetPtr(Target *target) {
273 m_target_sp = target->shared_from_this();
278 void ExecutionContext::SetProcessPtr(Process *process) {
280 m_process_sp = process->shared_from_this();
282 m_process_sp.reset();
285 void ExecutionContext::SetThreadPtr(Thread *thread) {
287 m_thread_sp = thread->shared_from_this();
292 void ExecutionContext::SetFramePtr(StackFrame *frame) {
294 m_frame_sp = frame->shared_from_this();
299 void ExecutionContext::SetContext(const lldb::TargetSP &target_sp,
301 m_target_sp = target_sp;
302 if (get_process && target_sp)
303 m_process_sp = target_sp->GetProcessSP();
305 m_process_sp.reset();
310 void ExecutionContext::SetContext(const lldb::ProcessSP &process_sp) {
311 m_process_sp = process_sp;
313 m_target_sp = process_sp->GetTarget().shared_from_this();
320 void ExecutionContext::SetContext(const lldb::ThreadSP &thread_sp) {
322 m_thread_sp = thread_sp;
324 m_process_sp = thread_sp->GetProcess();
326 m_target_sp = m_process_sp->GetTarget().shared_from_this();
331 m_process_sp.reset();
335 void ExecutionContext::SetContext(const lldb::StackFrameSP &frame_sp) {
336 m_frame_sp = frame_sp;
338 m_thread_sp = frame_sp->CalculateThread();
340 m_process_sp = m_thread_sp->GetProcess();
342 m_target_sp = m_process_sp->GetTarget().shared_from_this();
347 m_process_sp.reset();
351 m_process_sp.reset();
356 ExecutionContext &ExecutionContext::operator=(const ExecutionContext &rhs) {
358 m_target_sp = rhs.m_target_sp;
359 m_process_sp = rhs.m_process_sp;
360 m_thread_sp = rhs.m_thread_sp;
361 m_frame_sp = rhs.m_frame_sp;
366 bool ExecutionContext::operator==(const ExecutionContext &rhs) const {
367 // Check that the frame shared pointers match, or both are valid and their
368 // stack IDs match since sometimes we get new objects that represent the same
369 // frame within a thread.
370 if ((m_frame_sp == rhs.m_frame_sp) ||
371 (m_frame_sp && rhs.m_frame_sp &&
372 m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID())) {
373 // Check that the thread shared pointers match, or both are valid and their
374 // thread IDs match since sometimes we get new objects that represent the
375 // same thread within a process.
376 if ((m_thread_sp == rhs.m_thread_sp) ||
377 (m_thread_sp && rhs.m_thread_sp &&
378 m_thread_sp->GetID() == rhs.m_thread_sp->GetID())) {
379 // Processes and targets don't change much
380 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
386 bool ExecutionContext::operator!=(const ExecutionContext &rhs) const {
387 return !(*this == rhs);
390 bool ExecutionContext::HasTargetScope() const {
391 return ((bool)m_target_sp && m_target_sp->IsValid());
394 bool ExecutionContext::HasProcessScope() const {
395 return (HasTargetScope() && ((bool)m_process_sp && m_process_sp->IsValid()));
398 bool ExecutionContext::HasThreadScope() const {
399 return (HasProcessScope() && ((bool)m_thread_sp && m_thread_sp->IsValid()));
402 bool ExecutionContext::HasFrameScope() const {
403 return HasThreadScope() && m_frame_sp;
406 ExecutionContextRef::ExecutionContextRef()
407 : m_target_wp(), m_process_wp(), m_thread_wp(),
408 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {}
410 ExecutionContextRef::ExecutionContextRef(const ExecutionContext *exe_ctx)
411 : m_target_wp(), m_process_wp(), m_thread_wp(),
412 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {
417 ExecutionContextRef::ExecutionContextRef(const ExecutionContext &exe_ctx)
418 : m_target_wp(), m_process_wp(), m_thread_wp(),
419 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {
423 ExecutionContextRef::ExecutionContextRef(Target *target, bool adopt_selected)
424 : m_target_wp(), m_process_wp(), m_thread_wp(),
425 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {
426 SetTargetPtr(target, adopt_selected);
429 ExecutionContextRef::ExecutionContextRef(const ExecutionContextRef &rhs)
430 : m_target_wp(rhs.m_target_wp), m_process_wp(rhs.m_process_wp),
431 m_thread_wp(rhs.m_thread_wp), m_tid(rhs.m_tid),
432 m_stack_id(rhs.m_stack_id) {}
434 ExecutionContextRef &ExecutionContextRef::
435 operator=(const ExecutionContextRef &rhs) {
437 m_target_wp = rhs.m_target_wp;
438 m_process_wp = rhs.m_process_wp;
439 m_thread_wp = rhs.m_thread_wp;
441 m_stack_id = rhs.m_stack_id;
446 ExecutionContextRef &ExecutionContextRef::
447 operator=(const ExecutionContext &exe_ctx) {
448 m_target_wp = exe_ctx.GetTargetSP();
449 m_process_wp = exe_ctx.GetProcessSP();
450 lldb::ThreadSP thread_sp(exe_ctx.GetThreadSP());
451 m_thread_wp = thread_sp;
453 m_tid = thread_sp->GetID();
455 m_tid = LLDB_INVALID_THREAD_ID;
456 lldb::StackFrameSP frame_sp(exe_ctx.GetFrameSP());
458 m_stack_id = frame_sp->GetStackID();
464 void ExecutionContextRef::Clear() {
466 m_process_wp.reset();
471 ExecutionContextRef::~ExecutionContextRef() = default;
473 void ExecutionContextRef::SetTargetSP(const lldb::TargetSP &target_sp) {
474 m_target_wp = target_sp;
477 void ExecutionContextRef::SetProcessSP(const lldb::ProcessSP &process_sp) {
479 m_process_wp = process_sp;
480 SetTargetSP(process_sp->GetTarget().shared_from_this());
482 m_process_wp.reset();
487 void ExecutionContextRef::SetThreadSP(const lldb::ThreadSP &thread_sp) {
489 m_thread_wp = thread_sp;
490 m_tid = thread_sp->GetID();
491 SetProcessSP(thread_sp->GetProcess());
494 m_process_wp.reset();
499 void ExecutionContextRef::SetFrameSP(const lldb::StackFrameSP &frame_sp) {
501 m_stack_id = frame_sp->GetStackID();
502 SetThreadSP(frame_sp->GetThread());
506 m_process_wp.reset();
511 void ExecutionContextRef::SetTargetPtr(Target *target, bool adopt_selected) {
514 lldb::TargetSP target_sp(target->shared_from_this());
516 m_target_wp = target_sp;
517 if (adopt_selected) {
518 lldb::ProcessSP process_sp(target_sp->GetProcessSP());
520 m_process_wp = process_sp;
522 // Only fill in the thread and frame if our process is stopped
523 // Don't just check the state, since we might be in the middle of
525 Process::StopLocker stop_locker;
527 if (stop_locker.TryLock(&process_sp->GetRunLock()) &&
528 StateIsStoppedState(process_sp->GetState(), true)) {
529 lldb::ThreadSP thread_sp(
530 process_sp->GetThreadList().GetSelectedThread());
532 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
535 SetThreadSP(thread_sp);
536 lldb::StackFrameSP frame_sp(thread_sp->GetSelectedFrame());
538 frame_sp = thread_sp->GetStackFrameAtIndex(0);
540 SetFrameSP(frame_sp);
550 void ExecutionContextRef::SetProcessPtr(Process *process) {
552 SetProcessSP(process->shared_from_this());
554 m_process_wp.reset();
559 void ExecutionContextRef::SetThreadPtr(Thread *thread) {
561 SetThreadSP(thread->shared_from_this());
564 m_process_wp.reset();
569 void ExecutionContextRef::SetFramePtr(StackFrame *frame) {
571 SetFrameSP(frame->shared_from_this());
576 lldb::TargetSP ExecutionContextRef::GetTargetSP() const {
577 lldb::TargetSP target_sp(m_target_wp.lock());
578 if (target_sp && !target_sp->IsValid())
583 lldb::ProcessSP ExecutionContextRef::GetProcessSP() const {
584 lldb::ProcessSP process_sp(m_process_wp.lock());
585 if (process_sp && !process_sp->IsValid())
590 lldb::ThreadSP ExecutionContextRef::GetThreadSP() const {
591 lldb::ThreadSP thread_sp(m_thread_wp.lock());
593 if (m_tid != LLDB_INVALID_THREAD_ID) {
594 // We check if the thread has been destroyed in cases where clients might
595 // still have shared pointer to a thread, but the thread is not valid
596 // anymore (not part of the process)
597 if (!thread_sp || !thread_sp->IsValid()) {
598 lldb::ProcessSP process_sp(GetProcessSP());
599 if (process_sp && process_sp->IsValid()) {
600 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
601 m_thread_wp = thread_sp;
606 // Check that we aren't about to return an invalid thread sp. We might
607 // return a nullptr thread_sp, but don't return an invalid one.
609 if (thread_sp && !thread_sp->IsValid())
615 lldb::StackFrameSP ExecutionContextRef::GetFrameSP() const {
616 if (m_stack_id.IsValid()) {
617 lldb::ThreadSP thread_sp(GetThreadSP());
619 return thread_sp->GetFrameWithStackID(m_stack_id);
621 return lldb::StackFrameSP();
625 ExecutionContextRef::Lock(bool thread_and_frame_only_if_stopped) const {
626 return ExecutionContext(this, thread_and_frame_only_if_stopped);