blob: 20932ee63dce2b40912b0f96658b3a74bd52744d [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- ExecutionContext.cpp ----------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008
9#include "lldb/Target/ExecutionContext.h"
10#include "lldb/Target/ExecutionContextScope.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Target/Process.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Target/Target.h"
14#include "lldb/Target/Thread.h"
Pavel Labathd821c992018-08-07 11:07:21 +000015#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
17using namespace lldb_private;
18
Kate Stoneb9c1b512016-09-06 20:57:50 +000019ExecutionContext::ExecutionContext()
20 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {}
21
22ExecutionContext::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) {}
25
26ExecutionContext::ExecutionContext(const lldb::TargetSP &target_sp,
27 bool get_process)
28 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
29 if (target_sp)
30 SetContext(target_sp, get_process);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031}
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033ExecutionContext::ExecutionContext(const lldb::ProcessSP &process_sp)
34 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
35 if (process_sp)
36 SetContext(process_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039ExecutionContext::ExecutionContext(const lldb::ThreadSP &thread_sp)
40 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
41 if (thread_sp)
42 SetContext(thread_sp);
Greg Claytoncc4d0142012-02-17 07:49:44 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045ExecutionContext::ExecutionContext(const lldb::StackFrameSP &frame_sp)
46 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
47 if (frame_sp)
48 SetContext(frame_sp);
Greg Claytoncc4d0142012-02-17 07:49:44 +000049}
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051ExecutionContext::ExecutionContext(const lldb::TargetWP &target_wp,
52 bool get_process)
53 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
54 lldb::TargetSP target_sp(target_wp.lock());
55 if (target_sp)
56 SetContext(target_sp, get_process);
Greg Claytoncc4d0142012-02-17 07:49:44 +000057}
58
Kate Stoneb9c1b512016-09-06 20:57:50 +000059ExecutionContext::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());
62 if (process_sp)
63 SetContext(process_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +000064}
65
Kate Stoneb9c1b512016-09-06 20:57:50 +000066ExecutionContext::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());
69 if (thread_sp)
70 SetContext(thread_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +000071}
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073ExecutionContext::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());
76 if (frame_sp)
77 SetContext(frame_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080ExecutionContext::ExecutionContext(Target *t,
81 bool fill_current_process_thread_frame)
82 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
83 if (t) {
84 m_target_sp = t->shared_from_this();
85 if (fill_current_process_thread_frame) {
86 m_process_sp = t->GetProcessSP();
87 if (m_process_sp) {
88 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
89 if (m_thread_sp)
90 m_frame_sp = m_thread_sp->GetSelectedFrame();
91 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096ExecutionContext::ExecutionContext(Process *process, Thread *thread,
97 StackFrame *frame)
98 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
99 if (process) {
100 m_process_sp = process->shared_from_this();
101 m_target_sp = process->GetTarget().shared_from_this();
102 }
103 if (thread)
104 m_thread_sp = thread->shared_from_this();
105 if (frame)
106 m_frame_sp = frame->shared_from_this();
107}
108
109ExecutionContext::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()) {}
114
115ExecutionContext::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();
Greg Clayton526ae042015-02-12 00:34:25 +0000125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000127}
128
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000129ExecutionContext::ExecutionContext(const ExecutionContextRef *exe_ctx_ref_ptr,
130 std::unique_lock<std::recursive_mutex> &lock)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 : 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();
134 if (m_target_sp) {
135 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 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();
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000140 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000142}
143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144ExecutionContext::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(),
147 m_frame_sp() {
148 if (m_target_sp) {
149 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 m_process_sp = exe_ctx_ref.GetProcessSP();
152 m_thread_sp = exe_ctx_ref.GetThreadSP();
153 m_frame_sp = exe_ctx_ref.GetFrameSP();
154 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157ExecutionContext::ExecutionContext(ExecutionContextScope *exe_scope_ptr)
158 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
159 if (exe_scope_ptr)
160 exe_scope_ptr->CalculateExecutionContext(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163ExecutionContext::ExecutionContext(ExecutionContextScope &exe_scope_ref) {
164 exe_scope_ref.CalculateExecutionContext(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167void ExecutionContext::Clear() {
168 m_target_sp.reset();
169 m_process_sp.reset();
170 m_thread_sp.reset();
171 m_frame_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000172}
173
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000174ExecutionContext::~ExecutionContext() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176uint32_t ExecutionContext::GetAddressByteSize() const {
177 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
178 return m_target_sp->GetArchitecture().GetAddressByteSize();
179 if (m_process_sp)
180 return m_process_sp->GetAddressByteSize();
181 return sizeof(void *);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184lldb::ByteOrder ExecutionContext::GetByteOrder() const {
185 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
Jonas Devlieghereaf0c8282019-08-07 16:09:35 +0000186 return m_target_sp->GetArchitecture().GetByteOrder();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 if (m_process_sp)
Jonas Devlieghereaf0c8282019-08-07 16:09:35 +0000188 return m_process_sp->GetByteOrder();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 return endian::InlHostByteOrder();
Enrico Granata347c2aa2013-10-08 21:49:02 +0000190}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192RegisterContext *ExecutionContext::GetRegisterContext() const {
193 if (m_frame_sp)
194 return m_frame_sp->GetRegisterContext().get();
195 else if (m_thread_sp)
196 return m_thread_sp->GetRegisterContext().get();
197 return nullptr;
Greg Claytonc14ee322011-09-22 04:58:26 +0000198}
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200Target *ExecutionContext::GetTargetPtr() const {
201 if (m_target_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +0000202 return m_target_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 if (m_process_sp)
204 return &m_process_sp->GetTarget();
205 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
Greg Clayton644247c2011-07-07 01:59:51 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208Process *ExecutionContext::GetProcessPtr() const {
209 if (m_process_sp)
210 return m_process_sp.get();
211 if (m_target_sp)
212 return m_target_sp->GetProcessSP().get();
213 return nullptr;
214}
215
216ExecutionContextScope *ExecutionContext::GetBestExecutionContextScope() const {
217 if (m_frame_sp)
218 return m_frame_sp.get();
219 if (m_thread_sp)
220 return m_thread_sp.get();
221 if (m_process_sp)
222 return m_process_sp.get();
223 return m_target_sp.get();
224}
225
226Target &ExecutionContext::GetTargetRef() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 assert(m_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 return *m_target_sp;
Greg Clayton644247c2011-07-07 01:59:51 +0000229}
Greg Claytonc14ee322011-09-22 04:58:26 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231Process &ExecutionContext::GetProcessRef() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 assert(m_process_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 return *m_process_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +0000234}
235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236Thread &ExecutionContext::GetThreadRef() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 assert(m_thread_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 return *m_thread_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241StackFrame &ExecutionContext::GetFrameRef() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 assert(m_frame_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 return *m_frame_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246void ExecutionContext::SetTargetSP(const lldb::TargetSP &target_sp) {
247 m_target_sp = target_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +0000248}
249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250void ExecutionContext::SetProcessSP(const lldb::ProcessSP &process_sp) {
251 m_process_sp = process_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254void ExecutionContext::SetThreadSP(const lldb::ThreadSP &thread_sp) {
255 m_thread_sp = thread_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +0000256}
257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258void ExecutionContext::SetFrameSP(const lldb::StackFrameSP &frame_sp) {
259 m_frame_sp = frame_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +0000260}
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262void ExecutionContext::SetTargetPtr(Target *target) {
263 if (target)
264 m_target_sp = target->shared_from_this();
265 else
266 m_target_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269void ExecutionContext::SetProcessPtr(Process *process) {
270 if (process)
271 m_process_sp = process->shared_from_this();
272 else
273 m_process_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000274}
275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276void ExecutionContext::SetThreadPtr(Thread *thread) {
277 if (thread)
278 m_thread_sp = thread->shared_from_this();
279 else
Greg Claytoncc4d0142012-02-17 07:49:44 +0000280 m_thread_sp.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281}
282
283void ExecutionContext::SetFramePtr(StackFrame *frame) {
284 if (frame)
285 m_frame_sp = frame->shared_from_this();
286 else
Greg Claytoncc4d0142012-02-17 07:49:44 +0000287 m_frame_sp.reset();
288}
289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290void ExecutionContext::SetContext(const lldb::TargetSP &target_sp,
291 bool get_process) {
292 m_target_sp = target_sp;
293 if (get_process && target_sp)
294 m_process_sp = target_sp->GetProcessSP();
295 else
296 m_process_sp.reset();
297 m_thread_sp.reset();
298 m_frame_sp.reset();
299}
300
301void ExecutionContext::SetContext(const lldb::ProcessSP &process_sp) {
302 m_process_sp = process_sp;
303 if (process_sp)
304 m_target_sp = process_sp->GetTarget().shared_from_this();
305 else
306 m_target_sp.reset();
307 m_thread_sp.reset();
308 m_frame_sp.reset();
309}
310
311void ExecutionContext::SetContext(const lldb::ThreadSP &thread_sp) {
312 m_frame_sp.reset();
313 m_thread_sp = thread_sp;
314 if (thread_sp) {
315 m_process_sp = thread_sp->GetProcess();
316 if (m_process_sp)
317 m_target_sp = m_process_sp->GetTarget().shared_from_this();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000318 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 m_target_sp.reset();
320 } else {
321 m_target_sp.reset();
322 m_process_sp.reset();
323 }
324}
325
326void ExecutionContext::SetContext(const lldb::StackFrameSP &frame_sp) {
327 m_frame_sp = frame_sp;
328 if (frame_sp) {
329 m_thread_sp = frame_sp->CalculateThread();
330 if (m_thread_sp) {
331 m_process_sp = m_thread_sp->GetProcess();
332 if (m_process_sp)
333 m_target_sp = m_process_sp->GetTarget().shared_from_this();
334 else
Greg Claytoncc4d0142012-02-17 07:49:44 +0000335 m_target_sp.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 } else {
337 m_target_sp.reset();
338 m_process_sp.reset();
339 }
340 } else {
341 m_target_sp.reset();
342 m_process_sp.reset();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000343 m_thread_sp.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000345}
346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347ExecutionContext &ExecutionContext::operator=(const ExecutionContext &rhs) {
348 if (this != &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;
353 }
354 return *this;
355}
356
357bool ExecutionContext::operator==(const ExecutionContext &rhs) const {
358 // Check that the frame shared pointers match, or both are valid and their
Adrian Prantl05097242018-04-30 16:49:04 +0000359 // stack IDs match since sometimes we get new objects that represent the same
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 // 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())) {
Adrian Prantl05097242018-04-30 16:49:04 +0000364 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 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;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000372 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 }
374 return false;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000375}
376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377bool ExecutionContext::operator!=(const ExecutionContext &rhs) const {
378 return !(*this == rhs);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000379}
380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381bool ExecutionContext::HasTargetScope() const {
382 return ((bool)m_target_sp && m_target_sp->IsValid());
Greg Claytoncc4d0142012-02-17 07:49:44 +0000383}
384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385bool ExecutionContext::HasProcessScope() const {
386 return (HasTargetScope() && ((bool)m_process_sp && m_process_sp->IsValid()));
Greg Claytoncc4d0142012-02-17 07:49:44 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389bool ExecutionContext::HasThreadScope() const {
390 return (HasProcessScope() && ((bool)m_thread_sp && m_thread_sp->IsValid()));
Greg Claytoncc4d0142012-02-17 07:49:44 +0000391}
392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393bool ExecutionContext::HasFrameScope() const {
394 return HasThreadScope() && m_frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000395}
396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397ExecutionContextRef::ExecutionContextRef()
398 : m_target_wp(), m_process_wp(), m_thread_wp(),
399 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {}
400
401ExecutionContextRef::ExecutionContextRef(const ExecutionContext *exe_ctx)
402 : m_target_wp(), m_process_wp(), m_thread_wp(),
403 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {
404 if (exe_ctx)
405 *this = *exe_ctx;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000406}
407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408ExecutionContextRef::ExecutionContextRef(const ExecutionContext &exe_ctx)
409 : m_target_wp(), m_process_wp(), m_thread_wp(),
410 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {
411 *this = exe_ctx;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000412}
413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414ExecutionContextRef::ExecutionContextRef(Target *target, bool adopt_selected)
415 : m_target_wp(), m_process_wp(), m_thread_wp(),
416 m_tid(LLDB_INVALID_THREAD_ID), m_stack_id() {
417 SetTargetPtr(target, adopt_selected);
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000418}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420ExecutionContextRef::ExecutionContextRef(const ExecutionContextRef &rhs)
421 : m_target_wp(rhs.m_target_wp), m_process_wp(rhs.m_process_wp),
422 m_thread_wp(rhs.m_thread_wp), m_tid(rhs.m_tid),
423 m_stack_id(rhs.m_stack_id) {}
424
425ExecutionContextRef &ExecutionContextRef::
426operator=(const ExecutionContextRef &rhs) {
427 if (this != &rhs) {
428 m_target_wp = rhs.m_target_wp;
429 m_process_wp = rhs.m_process_wp;
430 m_thread_wp = rhs.m_thread_wp;
431 m_tid = rhs.m_tid;
432 m_stack_id = rhs.m_stack_id;
433 }
434 return *this;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000435}
436
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437ExecutionContextRef &ExecutionContextRef::
438operator=(const ExecutionContext &exe_ctx) {
439 m_target_wp = exe_ctx.GetTargetSP();
440 m_process_wp = exe_ctx.GetProcessSP();
441 lldb::ThreadSP thread_sp(exe_ctx.GetThreadSP());
442 m_thread_wp = thread_sp;
443 if (thread_sp)
444 m_tid = thread_sp->GetID();
445 else
446 m_tid = LLDB_INVALID_THREAD_ID;
447 lldb::StackFrameSP frame_sp(exe_ctx.GetFrameSP());
448 if (frame_sp)
449 m_stack_id = frame_sp->GetStackID();
450 else
451 m_stack_id.Clear();
452 return *this;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000453}
454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455void ExecutionContextRef::Clear() {
456 m_target_wp.reset();
457 m_process_wp.reset();
458 ClearThread();
459 ClearFrame();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000460}
461
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000462ExecutionContextRef::~ExecutionContextRef() = default;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464void ExecutionContextRef::SetTargetSP(const lldb::TargetSP &target_sp) {
465 m_target_wp = target_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000466}
467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468void ExecutionContextRef::SetProcessSP(const lldb::ProcessSP &process_sp) {
469 if (process_sp) {
470 m_process_wp = process_sp;
471 SetTargetSP(process_sp->GetTarget().shared_from_this());
472 } else {
473 m_process_wp.reset();
474 m_target_wp.reset();
475 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000476}
477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478void ExecutionContextRef::SetThreadSP(const lldb::ThreadSP &thread_sp) {
479 if (thread_sp) {
480 m_thread_wp = thread_sp;
481 m_tid = thread_sp->GetID();
482 SetProcessSP(thread_sp->GetProcess());
483 } else {
484 ClearThread();
485 m_process_wp.reset();
486 m_target_wp.reset();
487 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000488}
489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490void ExecutionContextRef::SetFrameSP(const lldb::StackFrameSP &frame_sp) {
491 if (frame_sp) {
492 m_stack_id = frame_sp->GetStackID();
493 SetThreadSP(frame_sp->GetThread());
494 } else {
495 ClearFrame();
496 ClearThread();
497 m_process_wp.reset();
498 m_target_wp.reset();
499 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000500}
501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502void ExecutionContextRef::SetTargetPtr(Target *target, bool adopt_selected) {
503 Clear();
504 if (target) {
505 lldb::TargetSP target_sp(target->shared_from_this());
506 if (target_sp) {
507 m_target_wp = target_sp;
508 if (adopt_selected) {
509 lldb::ProcessSP process_sp(target_sp->GetProcessSP());
510 if (process_sp) {
511 m_process_wp = process_sp;
512 if (process_sp) {
513 // Only fill in the thread and frame if our process is stopped
514 // Don't just check the state, since we might be in the middle of
515 // resuming.
516 Process::StopLocker stop_locker;
517
518 if (stop_locker.TryLock(&process_sp->GetRunLock()) &&
519 StateIsStoppedState(process_sp->GetState(), true)) {
520 lldb::ThreadSP thread_sp(
521 process_sp->GetThreadList().GetSelectedThread());
522 if (!thread_sp)
523 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
524
525 if (thread_sp) {
526 SetThreadSP(thread_sp);
527 lldb::StackFrameSP frame_sp(thread_sp->GetSelectedFrame());
528 if (!frame_sp)
529 frame_sp = thread_sp->GetStackFrameAtIndex(0);
530 if (frame_sp)
531 SetFrameSP(frame_sp);
532 }
533 }
534 }
535 }
536 }
537 }
538 }
539}
540
541void ExecutionContextRef::SetProcessPtr(Process *process) {
542 if (process) {
543 SetProcessSP(process->shared_from_this());
544 } else {
545 m_process_wp.reset();
546 m_target_wp.reset();
547 }
548}
549
550void ExecutionContextRef::SetThreadPtr(Thread *thread) {
551 if (thread) {
552 SetThreadSP(thread->shared_from_this());
553 } else {
554 ClearThread();
555 m_process_wp.reset();
556 m_target_wp.reset();
557 }
558}
559
560void ExecutionContextRef::SetFramePtr(StackFrame *frame) {
561 if (frame)
562 SetFrameSP(frame->shared_from_this());
563 else
Greg Claytoncc4d0142012-02-17 07:49:44 +0000564 Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000565}
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567lldb::TargetSP ExecutionContextRef::GetTargetSP() const {
568 lldb::TargetSP target_sp(m_target_wp.lock());
569 if (target_sp && !target_sp->IsValid())
570 target_sp.reset();
571 return target_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000572}
573
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574lldb::ProcessSP ExecutionContextRef::GetProcessSP() const {
575 lldb::ProcessSP process_sp(m_process_wp.lock());
576 if (process_sp && !process_sp->IsValid())
577 process_sp.reset();
578 return process_sp;
579}
580
581lldb::ThreadSP ExecutionContextRef::GetThreadSP() const {
582 lldb::ThreadSP thread_sp(m_thread_wp.lock());
583
584 if (m_tid != LLDB_INVALID_THREAD_ID) {
Adrian Prantl05097242018-04-30 16:49:04 +0000585 // We check if the thread has been destroyed in cases where clients might
586 // still have shared pointer to a thread, but the thread is not valid
587 // anymore (not part of the process)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 if (!thread_sp || !thread_sp->IsValid()) {
589 lldb::ProcessSP process_sp(GetProcessSP());
590 if (process_sp && process_sp->IsValid()) {
591 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
592 m_thread_wp = thread_sp;
593 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000594 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 }
596
Adrian Prantl05097242018-04-30 16:49:04 +0000597 // Check that we aren't about to return an invalid thread sp. We might
598 // return a nullptr thread_sp, but don't return an invalid one.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599
600 if (thread_sp && !thread_sp->IsValid())
601 thread_sp.reset();
602
603 return thread_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000604}
605
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606lldb::StackFrameSP ExecutionContextRef::GetFrameSP() const {
607 if (m_stack_id.IsValid()) {
608 lldb::ThreadSP thread_sp(GetThreadSP());
609 if (thread_sp)
610 return thread_sp->GetFrameWithStackID(m_stack_id);
611 }
612 return lldb::StackFrameSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000613}
614
615ExecutionContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616ExecutionContextRef::Lock(bool thread_and_frame_only_if_stopped) const {
617 return ExecutionContext(this, thread_and_frame_only_if_stopped);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000618}