blob: 41f076a205e929dfb004942eb787be22480d86b2 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ExecutionContext.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009
Eugene Zelenko9394d7722016-02-18 00:10:17 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Target/ExecutionContext.h"
Greg Clayton9b5450f2012-07-30 22:05:39 +000015#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Target/ExecutionContextScope.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000017#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
20#include "lldb/Target/Thread.h"
21
22using namespace lldb_private;
23
24ExecutionContext::ExecutionContext() :
Greg Claytonc14ee322011-09-22 04:58:26 +000025 m_target_sp (),
26 m_process_sp (),
27 m_thread_sp (),
28 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029{
30}
31
Greg Claytonc14ee322011-09-22 04:58:26 +000032ExecutionContext::ExecutionContext (const ExecutionContext &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +000033 m_target_sp(rhs.m_target_sp),
Greg Claytonc14ee322011-09-22 04:58:26 +000034 m_process_sp(rhs.m_process_sp),
Greg Claytoncc4d0142012-02-17 07:49:44 +000035 m_thread_sp(rhs.m_thread_sp),
36 m_frame_sp(rhs.m_frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +000037{
38}
39
Greg Claytoncc4d0142012-02-17 07:49:44 +000040ExecutionContext::ExecutionContext (const lldb::TargetSP &target_sp, bool get_process) :
41 m_target_sp (),
42 m_process_sp (),
43 m_thread_sp (),
44 m_frame_sp ()
Greg Claytonc14ee322011-09-22 04:58:26 +000045{
Greg Claytoncc4d0142012-02-17 07:49:44 +000046 if (target_sp)
47 SetContext (target_sp, get_process);
48}
49
50ExecutionContext::ExecutionContext (const lldb::ProcessSP &process_sp) :
51 m_target_sp (),
52 m_process_sp (),
53 m_thread_sp (),
54 m_frame_sp ()
55{
56 if (process_sp)
57 SetContext (process_sp);
58}
59
60ExecutionContext::ExecutionContext (const lldb::ThreadSP &thread_sp) :
61 m_target_sp (),
62 m_process_sp (),
63 m_thread_sp (),
64 m_frame_sp ()
65{
66 if (thread_sp)
67 SetContext (thread_sp);
68}
69
Jason Molendab57e4a12013-11-04 09:33:30 +000070ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
Greg Claytoncc4d0142012-02-17 07:49:44 +000071 m_target_sp (),
72 m_process_sp (),
73 m_thread_sp (),
74 m_frame_sp ()
75{
76 if (frame_sp)
77 SetContext (frame_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +000078}
79
Greg Clayton1ac04c32012-02-21 00:09:25 +000080ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
81 m_target_sp (),
82 m_process_sp (),
83 m_thread_sp (),
84 m_frame_sp ()
85{
86 lldb::TargetSP target_sp(target_wp.lock());
87 if (target_sp)
88 SetContext (target_sp, get_process);
89}
90
91ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
92 m_target_sp (),
93 m_process_sp (),
94 m_thread_sp (),
95 m_frame_sp ()
96{
97 lldb::ProcessSP process_sp(process_wp.lock());
98 if (process_sp)
99 SetContext (process_sp);
100}
101
102ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
103 m_target_sp (),
104 m_process_sp (),
105 m_thread_sp (),
106 m_frame_sp ()
107{
108 lldb::ThreadSP thread_sp(thread_wp.lock());
109 if (thread_sp)
110 SetContext (thread_sp);
111}
112
Jason Molendab57e4a12013-11-04 09:33:30 +0000113ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
Greg Clayton1ac04c32012-02-21 00:09:25 +0000114 m_target_sp (),
115 m_process_sp (),
116 m_thread_sp (),
117 m_frame_sp ()
118{
Jason Molendab57e4a12013-11-04 09:33:30 +0000119 lldb::StackFrameSP frame_sp(frame_wp.lock());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000120 if (frame_sp)
121 SetContext (frame_sp);
122}
123
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
Greg Clayton526ae042015-02-12 00:34:25 +0000125 m_target_sp (),
Greg Claytonc14ee322011-09-22 04:58:26 +0000126 m_process_sp (),
127 m_thread_sp (),
128 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129{
Greg Clayton526ae042015-02-12 00:34:25 +0000130 if (t)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 {
Greg Clayton526ae042015-02-12 00:34:25 +0000132 m_target_sp = t->shared_from_this();
133 if (fill_current_process_thread_frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 {
Greg Clayton526ae042015-02-12 00:34:25 +0000135 m_process_sp = t->GetProcessSP();
136 if (m_process_sp)
137 {
138 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
139 if (m_thread_sp)
140 m_frame_sp = m_thread_sp->GetSelectedFrame();
141 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 }
143 }
144}
145
Jason Molendab57e4a12013-11-04 09:33:30 +0000146ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
Greg Claytone1cd1be2012-01-29 20:56:30 +0000147 m_target_sp (),
Greg Clayton526ae042015-02-12 00:34:25 +0000148 m_process_sp (),
149 m_thread_sp (),
150 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000152 if (process)
Greg Clayton526ae042015-02-12 00:34:25 +0000153 {
154 m_process_sp = process->shared_from_this();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000155 m_target_sp = process->GetTarget().shared_from_this();
Greg Clayton526ae042015-02-12 00:34:25 +0000156 }
157 if (thread)
158 m_thread_sp = thread->shared_from_this();
159 if (frame)
160 m_frame_sp = frame->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161}
162
Greg Claytoncc4d0142012-02-17 07:49:44 +0000163ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref) :
164 m_target_sp (exe_ctx_ref.GetTargetSP()),
165 m_process_sp (exe_ctx_ref.GetProcessSP()),
166 m_thread_sp (exe_ctx_ref.GetThreadSP()),
167 m_frame_sp (exe_ctx_ref.GetFrameSP())
168{
169}
170
Greg Clayton44d93782014-01-27 23:43:24 +0000171ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, bool thread_and_frame_only_if_stopped) :
Greg Claytoncc4d0142012-02-17 07:49:44 +0000172 m_target_sp (),
173 m_process_sp (),
174 m_thread_sp (),
175 m_frame_sp ()
176{
177 if (exe_ctx_ref_ptr)
178 {
179 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
180 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
Greg Clayton44d93782014-01-27 23:43:24 +0000181 if (!thread_and_frame_only_if_stopped || (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true)))
182 {
183 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
184 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
185 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000186 }
187}
188
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000189ExecutionContext::ExecutionContext(const ExecutionContextRef *exe_ctx_ref_ptr,
190 std::unique_lock<std::recursive_mutex> &lock)
191 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp()
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000192{
193 if (exe_ctx_ref_ptr)
194 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000195 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000196 if (m_target_sp)
197 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000198 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
199
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000200 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
201 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
202 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
203 }
204 }
205}
206
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000207ExecutionContext::ExecutionContext(const ExecutionContextRef &exe_ctx_ref, std::unique_lock<std::recursive_mutex> &lock)
208 : m_target_sp(exe_ctx_ref.GetTargetSP()), m_process_sp(), m_thread_sp(), m_frame_sp()
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000209{
210 if (m_target_sp)
211 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000212 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
213
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000214 m_process_sp = exe_ctx_ref.GetProcessSP();
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000215 m_thread_sp = exe_ctx_ref.GetThreadSP();
216 m_frame_sp = exe_ctx_ref.GetFrameSP();
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000217 }
218}
219
Greg Claytoncc4d0142012-02-17 07:49:44 +0000220ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
221 m_target_sp (),
222 m_process_sp (),
223 m_thread_sp (),
224 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225{
226 if (exe_scope_ptr)
Greg Clayton0603aa92010-10-04 01:05:56 +0000227 exe_scope_ptr->CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
230ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
231{
Greg Clayton0603aa92010-10-04 01:05:56 +0000232 exe_scope_ref.CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}
234
235void
236ExecutionContext::Clear()
237{
Greg Claytonc14ee322011-09-22 04:58:26 +0000238 m_target_sp.reset();
239 m_process_sp.reset();
240 m_thread_sp.reset();
241 m_frame_sp.reset();
242}
243
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000244ExecutionContext::~ExecutionContext() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245
Greg Claytoncc4d0142012-02-17 07:49:44 +0000246uint32_t
247ExecutionContext::GetAddressByteSize() const
248{
249 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
Hafiz Abid Qadeered69e302015-02-25 16:01:12 +0000250 return m_target_sp->GetArchitecture().GetAddressByteSize();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000251 if (m_process_sp)
Hafiz Abid Qadeered69e302015-02-25 16:01:12 +0000252 return m_process_sp->GetAddressByteSize();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000253 return sizeof(void *);
254}
255
Enrico Granata347c2aa2013-10-08 21:49:02 +0000256lldb::ByteOrder
257ExecutionContext::GetByteOrder() const
258{
259 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
260 m_target_sp->GetArchitecture().GetByteOrder();
261 if (m_process_sp)
262 m_process_sp->GetByteOrder();
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000263 return endian::InlHostByteOrder();
Enrico Granata347c2aa2013-10-08 21:49:02 +0000264}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265
266RegisterContext *
267ExecutionContext::GetRegisterContext () const
268{
Greg Claytonc14ee322011-09-22 04:58:26 +0000269 if (m_frame_sp)
270 return m_frame_sp->GetRegisterContext().get();
271 else if (m_thread_sp)
272 return m_thread_sp->GetRegisterContext().get();
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000273 return nullptr;
Greg Claytonc14ee322011-09-22 04:58:26 +0000274}
275
276Target *
277ExecutionContext::GetTargetPtr () const
278{
279 if (m_target_sp)
280 return m_target_sp.get();
281 if (m_process_sp)
282 return &m_process_sp->GetTarget();
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000283 return nullptr;
Greg Claytonc14ee322011-09-22 04:58:26 +0000284}
285
286Process *
287ExecutionContext::GetProcessPtr () const
288{
289 if (m_process_sp)
290 return m_process_sp.get();
291 if (m_target_sp)
292 return m_target_sp->GetProcessSP().get();
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000293 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294}
295
296ExecutionContextScope *
297ExecutionContext::GetBestExecutionContextScope () const
298{
Greg Claytonc14ee322011-09-22 04:58:26 +0000299 if (m_frame_sp)
300 return m_frame_sp.get();
301 if (m_thread_sp)
302 return m_thread_sp.get();
303 if (m_process_sp)
304 return m_process_sp.get();
305 return m_target_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
Greg Clayton644247c2011-07-07 01:59:51 +0000307
Greg Claytonc14ee322011-09-22 04:58:26 +0000308Target &
309ExecutionContext::GetTargetRef () const
Greg Clayton644247c2011-07-07 01:59:51 +0000310{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000311#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000312 assert (m_target_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000313#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000314 return *m_target_sp;
Greg Clayton644247c2011-07-07 01:59:51 +0000315}
Greg Claytonc14ee322011-09-22 04:58:26 +0000316
317Process &
318ExecutionContext::GetProcessRef () const
319{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000320#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000321 assert (m_process_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000322#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000323 return *m_process_sp;
324}
325
326Thread &
327ExecutionContext::GetThreadRef () const
328{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000329#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000330 assert (m_thread_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000331#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000332 return *m_thread_sp;
333}
334
Jason Molendab57e4a12013-11-04 09:33:30 +0000335StackFrame &
Greg Claytonc14ee322011-09-22 04:58:26 +0000336ExecutionContext::GetFrameRef () const
337{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000338#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000339 assert (m_frame_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000340#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000341 return *m_frame_sp;
342}
343
344void
345ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
346{
347 m_target_sp = target_sp;
348}
349
350void
351ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
352{
353 m_process_sp = process_sp;
354}
355
356void
357ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
358{
359 m_thread_sp = thread_sp;
360}
361
362void
Jason Molendab57e4a12013-11-04 09:33:30 +0000363ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +0000364{
365 m_frame_sp = frame_sp;
366}
367
368void
369ExecutionContext::SetTargetPtr (Target* target)
370{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000371 if (target)
372 m_target_sp = target->shared_from_this();
373 else
374 m_target_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000375}
376
377void
378ExecutionContext::SetProcessPtr (Process *process)
379{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000380 if (process)
381 m_process_sp = process->shared_from_this();
382 else
383 m_process_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000384}
385
386void
387ExecutionContext::SetThreadPtr (Thread *thread)
388{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000389 if (thread)
390 m_thread_sp = thread->shared_from_this();
391 else
392 m_thread_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000393}
394
395void
Jason Molendab57e4a12013-11-04 09:33:30 +0000396ExecutionContext::SetFramePtr (StackFrame *frame)
Greg Claytonc14ee322011-09-22 04:58:26 +0000397{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000398 if (frame)
399 m_frame_sp = frame->shared_from_this();
400 else
401 m_frame_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000402}
403
Greg Claytoncc4d0142012-02-17 07:49:44 +0000404void
405ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
406{
407 m_target_sp = target_sp;
408 if (get_process && target_sp)
409 m_process_sp = target_sp->GetProcessSP();
410 else
411 m_process_sp.reset();
412 m_thread_sp.reset();
413 m_frame_sp.reset();
414}
415
416void
417ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
418{
419 m_process_sp = process_sp;
420 if (process_sp)
421 m_target_sp = process_sp->GetTarget().shared_from_this();
422 else
423 m_target_sp.reset();
424 m_thread_sp.reset();
425 m_frame_sp.reset();
426}
427
428void
429ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
430{
431 m_frame_sp.reset();
432 m_thread_sp = thread_sp;
433 if (thread_sp)
434 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000435 m_process_sp = thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000436 if (m_process_sp)
437 m_target_sp = m_process_sp->GetTarget().shared_from_this();
438 else
439 m_target_sp.reset();
440 }
441 else
442 {
443 m_target_sp.reset();
444 m_process_sp.reset();
445 }
446}
447
448void
Jason Molendab57e4a12013-11-04 09:33:30 +0000449ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000450{
451 m_frame_sp = frame_sp;
452 if (frame_sp)
453 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000454 m_thread_sp = frame_sp->CalculateThread();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000455 if (m_thread_sp)
456 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000457 m_process_sp = m_thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000458 if (m_process_sp)
459 m_target_sp = m_process_sp->GetTarget().shared_from_this();
460 else
461 m_target_sp.reset();
462 }
463 else
464 {
465 m_target_sp.reset();
466 m_process_sp.reset();
467 }
468 }
469 else
470 {
471 m_target_sp.reset();
472 m_process_sp.reset();
473 m_thread_sp.reset();
474 }
475}
476
477ExecutionContext &
478ExecutionContext::operator =(const ExecutionContext &rhs)
479{
480 if (this != &rhs)
481 {
482 m_target_sp = rhs.m_target_sp;
483 m_process_sp = rhs.m_process_sp;
484 m_thread_sp = rhs.m_thread_sp;
485 m_frame_sp = rhs.m_frame_sp;
486 }
487 return *this;
488}
489
490bool
491ExecutionContext::operator ==(const ExecutionContext &rhs) const
492{
493 // Check that the frame shared pointers match, or both are valid and their stack
494 // IDs match since sometimes we get new objects that represent the same
495 // frame within a thread.
496 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
497 {
498 // Check that the thread shared pointers match, or both are valid and
499 // their thread IDs match since sometimes we get new objects that
500 // represent the same thread within a process.
501 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
502 {
503 // Processes and targets don't change much
504 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
505 }
506 }
507 return false;
508}
509
510bool
511ExecutionContext::operator !=(const ExecutionContext &rhs) const
512{
513 return !(*this == rhs);
514}
515
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000516bool
517ExecutionContext::HasTargetScope () const
518{
519 return ((bool) m_target_sp
520 && m_target_sp->IsValid());
521}
522
523bool
524ExecutionContext::HasProcessScope () const
525{
526 return (HasTargetScope()
527 && ((bool) m_process_sp && m_process_sp->IsValid()));
528}
529
530bool
531ExecutionContext::HasThreadScope () const
532{
533 return (HasProcessScope()
534 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
535}
536
537bool
538ExecutionContext::HasFrameScope () const
539{
540 return HasThreadScope() && m_frame_sp;
541}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000542
543ExecutionContextRef::ExecutionContextRef() :
544 m_target_wp (),
545 m_process_wp (),
546 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000547 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000548 m_stack_id ()
549{
550}
551
552ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
553 m_target_wp (),
554 m_process_wp (),
555 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000556 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000557 m_stack_id ()
558{
559 if (exe_ctx)
560 *this = *exe_ctx;
561}
562
563ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
564 m_target_wp (),
565 m_process_wp (),
566 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000567 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000568 m_stack_id ()
569{
570 *this = exe_ctx;
571}
572
Greg Claytoncc4d0142012-02-17 07:49:44 +0000573ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
574 m_target_wp(),
575 m_process_wp(),
576 m_thread_wp(),
Greg Clayton6482d232013-03-15 23:54:07 +0000577 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000578 m_stack_id ()
579{
580 SetTargetPtr (target, adopt_selected);
581}
582
Greg Claytoncc4d0142012-02-17 07:49:44 +0000583ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
584 m_target_wp (rhs.m_target_wp),
585 m_process_wp(rhs.m_process_wp),
586 m_thread_wp (rhs.m_thread_wp),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000587 m_tid (rhs.m_tid),
588 m_stack_id (rhs.m_stack_id)
589{
590}
591
592ExecutionContextRef &
593ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
594{
595 if (this != &rhs)
596 {
597 m_target_wp = rhs.m_target_wp;
598 m_process_wp = rhs.m_process_wp;
599 m_thread_wp = rhs.m_thread_wp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000600 m_tid = rhs.m_tid;
601 m_stack_id = rhs.m_stack_id;
602 }
603 return *this;
604}
605
606ExecutionContextRef &
607ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
608{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000609 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000610 m_process_wp = exe_ctx.GetProcessSP();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000611 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
612 m_thread_wp = thread_sp;
613 if (thread_sp)
614 m_tid = thread_sp->GetID();
615 else
616 m_tid = LLDB_INVALID_THREAD_ID;
Jason Molendab57e4a12013-11-04 09:33:30 +0000617 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000618 if (frame_sp)
619 m_stack_id = frame_sp->GetStackID();
620 else
621 m_stack_id.Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000622 return *this;
623}
624
625void
626ExecutionContextRef::Clear()
627{
628 m_target_wp.reset();
629 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000630 ClearThread();
631 ClearFrame();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000632}
633
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000634ExecutionContextRef::~ExecutionContextRef() = default;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000635
636void
637ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
638{
639 m_target_wp = target_sp;
640}
641
642void
643ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
644{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000645 if (process_sp)
646 {
647 m_process_wp = process_sp;
648 SetTargetSP (process_sp->GetTarget().shared_from_this());
649 }
650 else
651 {
652 m_process_wp.reset();
653 m_target_wp.reset();
654 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000655}
656
657void
658ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
659{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000660 if (thread_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000661 {
662 m_thread_wp = thread_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000663 m_tid = thread_sp->GetID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000664 SetProcessSP (thread_sp->GetProcess());
665 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000666 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000667 {
668 ClearThread();
669 m_process_wp.reset();
670 m_target_wp.reset();
671 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000672}
673
674void
Jason Molendab57e4a12013-11-04 09:33:30 +0000675ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000676{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000677 if (frame_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000678 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000679 m_stack_id = frame_sp->GetStackID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000680 SetThreadSP (frame_sp->GetThread());
681 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000682 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000683 {
684 ClearFrame();
685 ClearThread();
686 m_process_wp.reset();
687 m_target_wp.reset();
688 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000689}
690
691void
692ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
693{
694 Clear();
695 if (target)
696 {
697 lldb::TargetSP target_sp (target->shared_from_this());
698 if (target_sp)
699 {
700 m_target_wp = target_sp;
701 if (adopt_selected)
702 {
703 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
704 if (process_sp)
705 {
706 m_process_wp = process_sp;
707 if (process_sp)
708 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000709 // Only fill in the thread and frame if our process is stopped
Jim Inghamd5ac1ab2015-01-19 23:51:51 +0000710 // Don't just check the state, since we might be in the middle of
711 // resuming.
712 Process::StopLocker stop_locker;
713
714 if (stop_locker.TryLock(&process_sp->GetRunLock()) && StateIsStoppedState (process_sp->GetState(), true))
Greg Claytoncc4d0142012-02-17 07:49:44 +0000715 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000716 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
717 if (!thread_sp)
718 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
719
720 if (thread_sp)
721 {
722 SetThreadSP (thread_sp);
Jason Molendab57e4a12013-11-04 09:33:30 +0000723 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
Greg Clayton9b5450f2012-07-30 22:05:39 +0000724 if (!frame_sp)
725 frame_sp = thread_sp->GetStackFrameAtIndex(0);
726 if (frame_sp)
727 SetFrameSP (frame_sp);
728 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000729 }
730 }
731 }
732 }
733 }
734 }
735}
736
737void
738ExecutionContextRef::SetProcessPtr (Process *process)
739{
740 if (process)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000741 {
742 SetProcessSP(process->shared_from_this());
743 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000744 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000745 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000746 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000747 m_target_wp.reset();
748 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000749}
750
751void
752ExecutionContextRef::SetThreadPtr (Thread *thread)
753{
754 if (thread)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000755 {
756 SetThreadSP (thread->shared_from_this());
757 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000758 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000759 {
760 ClearThread();
761 m_process_wp.reset();
762 m_target_wp.reset();
763 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000764}
765
766void
Jason Molendab57e4a12013-11-04 09:33:30 +0000767ExecutionContextRef::SetFramePtr (StackFrame *frame)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000768{
769 if (frame)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000770 SetFrameSP (frame->shared_from_this());
Greg Claytoncc4d0142012-02-17 07:49:44 +0000771 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000772 Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000773}
774
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000775lldb::TargetSP
776ExecutionContextRef::GetTargetSP () const
777{
778 lldb::TargetSP target_sp(m_target_wp.lock());
779 if (target_sp && !target_sp->IsValid())
780 target_sp.reset();
781 return target_sp;
782}
783
784lldb::ProcessSP
785ExecutionContextRef::GetProcessSP () const
786{
787 lldb::ProcessSP process_sp(m_process_wp.lock());
788 if (process_sp && !process_sp->IsValid())
789 process_sp.reset();
790 return process_sp;
791}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000792
793lldb::ThreadSP
794ExecutionContextRef::GetThreadSP () const
795{
796 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000797
Greg Clayton0b88d812012-04-04 20:43:47 +0000798 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000799 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000800 // We check if the thread has been destroyed in cases where clients
801 // might still have shared pointer to a thread, but the thread is
802 // not valid anymore (not part of the process)
803 if (!thread_sp || !thread_sp->IsValid())
Greg Claytoncc4d0142012-02-17 07:49:44 +0000804 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000805 lldb::ProcessSP process_sp(GetProcessSP());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000806 if (process_sp && process_sp->IsValid())
Greg Clayton0b88d812012-04-04 20:43:47 +0000807 {
808 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
809 m_thread_wp = thread_sp;
810 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000811 }
812 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000813
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000814 // Check that we aren't about to return an invalid thread sp. We might return a nullptr thread_sp,
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000815 // but don't return an invalid one.
816
817 if (thread_sp && !thread_sp->IsValid())
818 thread_sp.reset();
819
Greg Claytoncc4d0142012-02-17 07:49:44 +0000820 return thread_sp;
821}
822
Jason Molendab57e4a12013-11-04 09:33:30 +0000823lldb::StackFrameSP
Greg Claytoncc4d0142012-02-17 07:49:44 +0000824ExecutionContextRef::GetFrameSP () const
825{
Greg Clayton7bcb93d2013-05-24 00:58:29 +0000826 if (m_stack_id.IsValid())
827 {
828 lldb::ThreadSP thread_sp (GetThreadSP());
829 if (thread_sp)
830 return thread_sp->GetFrameWithStackID (m_stack_id);
831 }
Jason Molendab57e4a12013-11-04 09:33:30 +0000832 return lldb::StackFrameSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000833}
834
835ExecutionContext
Greg Clayton44d93782014-01-27 23:43:24 +0000836ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
Greg Claytoncc4d0142012-02-17 07:49:44 +0000837{
Greg Clayton44d93782014-01-27 23:43:24 +0000838 return ExecutionContext(this, thread_and_frame_only_if_stopped);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000839}