1 //===-- ExecutionContext.cpp ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/ExecutionContext.h"
10 #include "lldb/Target/ExecutionContextScope.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/StackFrame.h"
13 #include "lldb/Target/Target.h"
14 #include "lldb/Target/Thread.h"
15 #include "lldb/Utility/State.h"
17 using namespace lldb_private
;
19 ExecutionContext::ExecutionContext()
20 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {}
22 ExecutionContext::ExecutionContext(const ExecutionContext
&rhs
)
23 : m_target_sp(rhs
.m_target_sp
), m_process_sp(rhs
.m_process_sp
),
24 m_thread_sp(rhs
.m_thread_sp
), m_frame_sp(rhs
.m_frame_sp
) {}
26 ExecutionContext::ExecutionContext(const lldb::TargetSP
&target_sp
,
28 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
30 SetContext(target_sp
, get_process
);
33 ExecutionContext::ExecutionContext(const lldb::ProcessSP
&process_sp
)
34 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
36 SetContext(process_sp
);
39 ExecutionContext::ExecutionContext(const lldb::ThreadSP
&thread_sp
)
40 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
42 SetContext(thread_sp
);
45 ExecutionContext::ExecutionContext(const lldb::StackFrameSP
&frame_sp
)
46 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
51 ExecutionContext::ExecutionContext(const lldb::TargetWP
&target_wp
,
53 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
54 lldb::TargetSP
target_sp(target_wp
.lock());
56 SetContext(target_sp
, get_process
);
59 ExecutionContext::ExecutionContext(const lldb::ProcessWP
&process_wp
)
60 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
61 lldb::ProcessSP
process_sp(process_wp
.lock());
63 SetContext(process_sp
);
66 ExecutionContext::ExecutionContext(const lldb::ThreadWP
&thread_wp
)
67 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
68 lldb::ThreadSP
thread_sp(thread_wp
.lock());
70 SetContext(thread_sp
);
73 ExecutionContext::ExecutionContext(const lldb::StackFrameWP
&frame_wp
)
74 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
75 lldb::StackFrameSP
frame_sp(frame_wp
.lock());
80 ExecutionContext::ExecutionContext(Target
*t
,
81 bool fill_current_process_thread_frame
)
82 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
84 m_target_sp
= t
->shared_from_this();
85 if (fill_current_process_thread_frame
) {
86 m_process_sp
= t
->GetProcessSP();
88 m_thread_sp
= m_process_sp
->GetThreadList().GetSelectedThread();
90 m_frame_sp
= m_thread_sp
->GetSelectedFrame();
96 ExecutionContext::ExecutionContext(Process
*process
, Thread
*thread
,
98 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
100 m_process_sp
= process
->shared_from_this();
101 m_target_sp
= process
->GetTarget().shared_from_this();
104 m_thread_sp
= thread
->shared_from_this();
106 m_frame_sp
= frame
->shared_from_this();
109 ExecutionContext::ExecutionContext(const ExecutionContextRef
&exe_ctx_ref
)
110 : m_target_sp(exe_ctx_ref
.GetTargetSP()),
111 m_process_sp(exe_ctx_ref
.GetProcessSP()),
112 m_thread_sp(exe_ctx_ref
.GetThreadSP()),
113 m_frame_sp(exe_ctx_ref
.GetFrameSP()) {}
115 ExecutionContext::ExecutionContext(const ExecutionContextRef
*exe_ctx_ref_ptr
,
116 bool thread_and_frame_only_if_stopped
)
117 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
118 if (exe_ctx_ref_ptr
) {
119 m_target_sp
= exe_ctx_ref_ptr
->GetTargetSP();
120 m_process_sp
= exe_ctx_ref_ptr
->GetProcessSP();
121 if (!thread_and_frame_only_if_stopped
||
122 (m_process_sp
&& StateIsStoppedState(m_process_sp
->GetState(), true))) {
123 m_thread_sp
= exe_ctx_ref_ptr
->GetThreadSP();
124 m_frame_sp
= exe_ctx_ref_ptr
->GetFrameSP();
129 ExecutionContext::ExecutionContext(const ExecutionContextRef
*exe_ctx_ref_ptr
,
130 std::unique_lock
<std::recursive_mutex
> &lock
)
131 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
132 if (exe_ctx_ref_ptr
) {
133 m_target_sp
= exe_ctx_ref_ptr
->GetTargetSP();
135 lock
= std::unique_lock
<std::recursive_mutex
>(m_target_sp
->GetAPIMutex());
137 m_process_sp
= exe_ctx_ref_ptr
->GetProcessSP();
138 m_thread_sp
= exe_ctx_ref_ptr
->GetThreadSP();
139 m_frame_sp
= exe_ctx_ref_ptr
->GetFrameSP();
144 ExecutionContext::ExecutionContext(const ExecutionContextRef
&exe_ctx_ref
,
145 std::unique_lock
<std::recursive_mutex
> &lock
)
146 : m_target_sp(exe_ctx_ref
.GetTargetSP()), m_process_sp(), m_thread_sp(),
149 lock
= std::unique_lock
<std::recursive_mutex
>(m_target_sp
->GetAPIMutex());
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(ExecutionContextScope
*exe_scope_ptr
)
158 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
160 exe_scope_ptr
->CalculateExecutionContext(*this);
163 ExecutionContext::ExecutionContext(ExecutionContextScope
&exe_scope_ref
) {
164 exe_scope_ref
.CalculateExecutionContext(*this);
167 void ExecutionContext::Clear() {
169 m_process_sp
.reset();
174 ExecutionContext::~ExecutionContext() = default;
176 uint32_t ExecutionContext::GetAddressByteSize() const {
177 if (m_target_sp
&& m_target_sp
->GetArchitecture().IsValid())
178 return m_target_sp
->GetArchitecture().GetAddressByteSize();
180 return m_process_sp
->GetAddressByteSize();
181 return sizeof(void *);
184 lldb::ByteOrder
ExecutionContext::GetByteOrder() const {
185 if (m_target_sp
&& m_target_sp
->GetArchitecture().IsValid())
186 return m_target_sp
->GetArchitecture().GetByteOrder();
188 return m_process_sp
->GetByteOrder();
189 return endian::InlHostByteOrder();
192 RegisterContext
*ExecutionContext::GetRegisterContext() const {
194 return m_frame_sp
->GetRegisterContext().get();
195 else if (m_thread_sp
)
196 return m_thread_sp
->GetRegisterContext().get();
200 Target
*ExecutionContext::GetTargetPtr() const {
202 return m_target_sp
.get();
204 return &m_process_sp
->GetTarget();
208 Process
*ExecutionContext::GetProcessPtr() const {
210 return m_process_sp
.get();
212 return m_target_sp
->GetProcessSP().get();
216 ExecutionContextScope
*ExecutionContext::GetBestExecutionContextScope() const {
218 return m_frame_sp
.get();
220 return m_thread_sp
.get();
222 return m_process_sp
.get();
223 return m_target_sp
.get();
226 Target
&ExecutionContext::GetTargetRef() const {
231 Process
&ExecutionContext::GetProcessRef() const {
232 assert(m_process_sp
);
233 return *m_process_sp
;
236 Thread
&ExecutionContext::GetThreadRef() const {
241 StackFrame
&ExecutionContext::GetFrameRef() const {
246 void ExecutionContext::SetTargetSP(const lldb::TargetSP
&target_sp
) {
247 m_target_sp
= target_sp
;
250 void ExecutionContext::SetProcessSP(const lldb::ProcessSP
&process_sp
) {
251 m_process_sp
= process_sp
;
254 void ExecutionContext::SetThreadSP(const lldb::ThreadSP
&thread_sp
) {
255 m_thread_sp
= thread_sp
;
258 void ExecutionContext::SetFrameSP(const lldb::StackFrameSP
&frame_sp
) {
259 m_frame_sp
= frame_sp
;
262 void ExecutionContext::SetTargetPtr(Target
*target
) {
264 m_target_sp
= target
->shared_from_this();
269 void ExecutionContext::SetProcessPtr(Process
*process
) {
271 m_process_sp
= process
->shared_from_this();
273 m_process_sp
.reset();
276 void ExecutionContext::SetThreadPtr(Thread
*thread
) {
278 m_thread_sp
= thread
->shared_from_this();
283 void ExecutionContext::SetFramePtr(StackFrame
*frame
) {
285 m_frame_sp
= frame
->shared_from_this();
290 void ExecutionContext::SetContext(const lldb::TargetSP
&target_sp
,
292 m_target_sp
= target_sp
;
293 if (get_process
&& target_sp
)
294 m_process_sp
= target_sp
->GetProcessSP();
296 m_process_sp
.reset();
301 void ExecutionContext::SetContext(const lldb::ProcessSP
&process_sp
) {
302 m_process_sp
= process_sp
;
304 m_target_sp
= process_sp
->GetTarget().shared_from_this();
311 void ExecutionContext::SetContext(const lldb::ThreadSP
&thread_sp
) {
313 m_thread_sp
= thread_sp
;
315 m_process_sp
= thread_sp
->GetProcess();
317 m_target_sp
= m_process_sp
->GetTarget().shared_from_this();
322 m_process_sp
.reset();
326 void ExecutionContext::SetContext(const lldb::StackFrameSP
&frame_sp
) {
327 m_frame_sp
= frame_sp
;
329 m_thread_sp
= frame_sp
->CalculateThread();
331 m_process_sp
= m_thread_sp
->GetProcess();
333 m_target_sp
= m_process_sp
->GetTarget().shared_from_this();
338 m_process_sp
.reset();
342 m_process_sp
.reset();
347 ExecutionContext
&ExecutionContext::operator=(const ExecutionContext
&rhs
) {
349 m_target_sp
= rhs
.m_target_sp
;
350 m_process_sp
= rhs
.m_process_sp
;
351 m_thread_sp
= rhs
.m_thread_sp
;
352 m_frame_sp
= rhs
.m_frame_sp
;
357 bool ExecutionContext::operator==(const ExecutionContext
&rhs
) const {
358 // Check that the frame shared pointers match, or both are valid and their
359 // stack IDs match since sometimes we get new objects that represent the same
360 // frame within a thread.
361 if ((m_frame_sp
== rhs
.m_frame_sp
) ||
362 (m_frame_sp
&& rhs
.m_frame_sp
&&
363 m_frame_sp
->GetStackID() == rhs
.m_frame_sp
->GetStackID())) {
364 // Check that the thread shared pointers match, or both are valid and their
365 // thread IDs match since sometimes we get new objects that represent the
366 // same thread within a process.
367 if ((m_thread_sp
== rhs
.m_thread_sp
) ||
368 (m_thread_sp
&& rhs
.m_thread_sp
&&
369 m_thread_sp
->GetID() == rhs
.m_thread_sp
->GetID())) {
370 // Processes and targets don't change much
371 return m_process_sp
== rhs
.m_process_sp
&& m_target_sp
== rhs
.m_target_sp
;
377 bool ExecutionContext::operator!=(const ExecutionContext
&rhs
) const {
378 return !(*this == rhs
);
381 bool ExecutionContext::HasTargetScope() const {
382 return ((bool)m_target_sp
&& m_target_sp
->IsValid());
385 bool ExecutionContext::HasProcessScope() const {
386 return (HasTargetScope() && ((bool)m_process_sp
&& m_process_sp
->IsValid()));
389 bool ExecutionContext::HasThreadScope() const {
390 return (HasProcessScope() && ((bool)m_thread_sp
&& m_thread_sp
->IsValid()));
393 bool ExecutionContext::HasFrameScope() const {
394 return HasThreadScope() && m_frame_sp
;
397 ExecutionContextRef::ExecutionContextRef()
398 : m_target_wp(), m_process_wp(), m_thread_wp(), m_stack_id() {}
400 ExecutionContextRef::ExecutionContextRef(const ExecutionContext
*exe_ctx
)
401 : m_target_wp(), m_process_wp(), m_thread_wp(),
402 m_tid(LLDB_INVALID_THREAD_ID
), m_stack_id() {
407 ExecutionContextRef::ExecutionContextRef(const ExecutionContext
&exe_ctx
)
408 : m_target_wp(), m_process_wp(), m_thread_wp(),
409 m_tid(LLDB_INVALID_THREAD_ID
), m_stack_id() {
413 ExecutionContextRef::ExecutionContextRef(Target
*target
, bool adopt_selected
)
414 : m_target_wp(), m_process_wp(), m_thread_wp(),
415 m_tid(LLDB_INVALID_THREAD_ID
), m_stack_id() {
416 SetTargetPtr(target
, adopt_selected
);
419 ExecutionContextRef::ExecutionContextRef(const ExecutionContextRef
&rhs
)
420 : m_target_wp(rhs
.m_target_wp
), m_process_wp(rhs
.m_process_wp
),
421 m_thread_wp(rhs
.m_thread_wp
), m_tid(rhs
.m_tid
),
422 m_stack_id(rhs
.m_stack_id
) {}
424 ExecutionContextRef
&ExecutionContextRef::
425 operator=(const ExecutionContextRef
&rhs
) {
427 m_target_wp
= rhs
.m_target_wp
;
428 m_process_wp
= rhs
.m_process_wp
;
429 m_thread_wp
= rhs
.m_thread_wp
;
431 m_stack_id
= rhs
.m_stack_id
;
436 ExecutionContextRef
&ExecutionContextRef::
437 operator=(const ExecutionContext
&exe_ctx
) {
438 m_target_wp
= exe_ctx
.GetTargetSP();
439 m_process_wp
= exe_ctx
.GetProcessSP();
440 lldb::ThreadSP
thread_sp(exe_ctx
.GetThreadSP());
441 m_thread_wp
= thread_sp
;
443 m_tid
= thread_sp
->GetID();
445 m_tid
= LLDB_INVALID_THREAD_ID
;
446 lldb::StackFrameSP
frame_sp(exe_ctx
.GetFrameSP());
448 m_stack_id
= frame_sp
->GetStackID();
454 void ExecutionContextRef::Clear() {
456 m_process_wp
.reset();
461 ExecutionContextRef::~ExecutionContextRef() = default;
463 void ExecutionContextRef::SetTargetSP(const lldb::TargetSP
&target_sp
) {
464 m_target_wp
= target_sp
;
467 void ExecutionContextRef::SetProcessSP(const lldb::ProcessSP
&process_sp
) {
469 m_process_wp
= process_sp
;
470 SetTargetSP(process_sp
->GetTarget().shared_from_this());
472 m_process_wp
.reset();
477 void ExecutionContextRef::SetThreadSP(const lldb::ThreadSP
&thread_sp
) {
479 m_thread_wp
= thread_sp
;
480 m_tid
= thread_sp
->GetID();
481 SetProcessSP(thread_sp
->GetProcess());
484 m_process_wp
.reset();
489 void ExecutionContextRef::SetFrameSP(const lldb::StackFrameSP
&frame_sp
) {
491 m_stack_id
= frame_sp
->GetStackID();
492 SetThreadSP(frame_sp
->GetThread());
496 m_process_wp
.reset();
501 void ExecutionContextRef::SetTargetPtr(Target
*target
, bool adopt_selected
) {
504 lldb::TargetSP
target_sp(target
->shared_from_this());
506 m_target_wp
= target_sp
;
507 if (adopt_selected
) {
508 lldb::ProcessSP
process_sp(target_sp
->GetProcessSP());
510 m_process_wp
= process_sp
;
512 // Only fill in the thread and frame if our process is stopped
513 // Don't just check the state, since we might be in the middle of
515 Process::StopLocker stop_locker
;
517 if (stop_locker
.TryLock(&process_sp
->GetRunLock()) &&
518 StateIsStoppedState(process_sp
->GetState(), true)) {
519 lldb::ThreadSP
thread_sp(
520 process_sp
->GetThreadList().GetSelectedThread());
522 thread_sp
= process_sp
->GetThreadList().GetThreadAtIndex(0);
525 SetThreadSP(thread_sp
);
526 lldb::StackFrameSP
frame_sp(thread_sp
->GetSelectedFrame());
528 frame_sp
= thread_sp
->GetStackFrameAtIndex(0);
530 SetFrameSP(frame_sp
);
540 void ExecutionContextRef::SetProcessPtr(Process
*process
) {
542 SetProcessSP(process
->shared_from_this());
544 m_process_wp
.reset();
549 void ExecutionContextRef::SetThreadPtr(Thread
*thread
) {
551 SetThreadSP(thread
->shared_from_this());
554 m_process_wp
.reset();
559 void ExecutionContextRef::SetFramePtr(StackFrame
*frame
) {
561 SetFrameSP(frame
->shared_from_this());
566 lldb::TargetSP
ExecutionContextRef::GetTargetSP() const {
567 lldb::TargetSP
target_sp(m_target_wp
.lock());
568 if (target_sp
&& !target_sp
->IsValid())
573 lldb::ProcessSP
ExecutionContextRef::GetProcessSP() const {
574 lldb::ProcessSP
process_sp(m_process_wp
.lock());
575 if (process_sp
&& !process_sp
->IsValid())
580 lldb::ThreadSP
ExecutionContextRef::GetThreadSP() const {
581 lldb::ThreadSP
thread_sp(m_thread_wp
.lock());
583 if (m_tid
!= LLDB_INVALID_THREAD_ID
) {
584 // We check if the thread has been destroyed in cases where clients might
585 // still have shared pointer to a thread, but the thread is not valid
586 // anymore (not part of the process)
587 if (!thread_sp
|| !thread_sp
->IsValid()) {
588 lldb::ProcessSP
process_sp(GetProcessSP());
589 if (process_sp
&& process_sp
->IsValid()) {
590 thread_sp
= process_sp
->GetThreadList().FindThreadByID(m_tid
);
591 m_thread_wp
= thread_sp
;
596 // Check that we aren't about to return an invalid thread sp. We might
597 // return a nullptr thread_sp, but don't return an invalid one.
599 if (thread_sp
&& !thread_sp
->IsValid())
605 lldb::StackFrameSP
ExecutionContextRef::GetFrameSP() const {
606 if (m_stack_id
.IsValid()) {
607 lldb::ThreadSP
thread_sp(GetThreadSP());
609 return thread_sp
->GetFrameWithStackID(m_stack_id
);
611 return lldb::StackFrameSP();
615 ExecutionContextRef::Lock(bool thread_and_frame_only_if_stopped
) const {
616 return ExecutionContext(this, thread_and_frame_only_if_stopped
);