blob: 0490e905217d38919be5a869808b24d64b760946 [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
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000189ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
190 m_target_sp (),
191 m_process_sp (),
192 m_thread_sp (),
193 m_frame_sp ()
194{
195 if (exe_ctx_ref_ptr)
196 {
197 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
198 if (m_target_sp)
199 {
200 locker.Lock(m_target_sp->GetAPIMutex());
201 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
202 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
203 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
204 }
205 }
206}
207
208ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
209 m_target_sp (exe_ctx_ref.GetTargetSP()),
210 m_process_sp (),
211 m_thread_sp (),
212 m_frame_sp ()
213{
214 if (m_target_sp)
215 {
216 locker.Lock(m_target_sp->GetAPIMutex());
217 m_process_sp = exe_ctx_ref.GetProcessSP();
218 m_thread_sp = exe_ctx_ref.GetThreadSP();
219 m_frame_sp = exe_ctx_ref.GetFrameSP();
220 }
221}
222
Greg Claytoncc4d0142012-02-17 07:49:44 +0000223ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
224 m_target_sp (),
225 m_process_sp (),
226 m_thread_sp (),
227 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228{
229 if (exe_scope_ptr)
Greg Clayton0603aa92010-10-04 01:05:56 +0000230 exe_scope_ptr->CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
233ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
234{
Greg Clayton0603aa92010-10-04 01:05:56 +0000235 exe_scope_ref.CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
238void
239ExecutionContext::Clear()
240{
Greg Claytonc14ee322011-09-22 04:58:26 +0000241 m_target_sp.reset();
242 m_process_sp.reset();
243 m_thread_sp.reset();
244 m_frame_sp.reset();
245}
246
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000247ExecutionContext::~ExecutionContext() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248
Greg Claytoncc4d0142012-02-17 07:49:44 +0000249uint32_t
250ExecutionContext::GetAddressByteSize() const
251{
252 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
Hafiz Abid Qadeered69e302015-02-25 16:01:12 +0000253 return m_target_sp->GetArchitecture().GetAddressByteSize();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000254 if (m_process_sp)
Hafiz Abid Qadeered69e302015-02-25 16:01:12 +0000255 return m_process_sp->GetAddressByteSize();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000256 return sizeof(void *);
257}
258
Enrico Granata347c2aa2013-10-08 21:49:02 +0000259lldb::ByteOrder
260ExecutionContext::GetByteOrder() const
261{
262 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
263 m_target_sp->GetArchitecture().GetByteOrder();
264 if (m_process_sp)
265 m_process_sp->GetByteOrder();
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000266 return endian::InlHostByteOrder();
Enrico Granata347c2aa2013-10-08 21:49:02 +0000267}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268
269RegisterContext *
270ExecutionContext::GetRegisterContext () const
271{
Greg Claytonc14ee322011-09-22 04:58:26 +0000272 if (m_frame_sp)
273 return m_frame_sp->GetRegisterContext().get();
274 else if (m_thread_sp)
275 return m_thread_sp->GetRegisterContext().get();
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000276 return nullptr;
Greg Claytonc14ee322011-09-22 04:58:26 +0000277}
278
279Target *
280ExecutionContext::GetTargetPtr () const
281{
282 if (m_target_sp)
283 return m_target_sp.get();
284 if (m_process_sp)
285 return &m_process_sp->GetTarget();
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000286 return nullptr;
Greg Claytonc14ee322011-09-22 04:58:26 +0000287}
288
289Process *
290ExecutionContext::GetProcessPtr () const
291{
292 if (m_process_sp)
293 return m_process_sp.get();
294 if (m_target_sp)
295 return m_target_sp->GetProcessSP().get();
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000296 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297}
298
299ExecutionContextScope *
300ExecutionContext::GetBestExecutionContextScope () const
301{
Greg Claytonc14ee322011-09-22 04:58:26 +0000302 if (m_frame_sp)
303 return m_frame_sp.get();
304 if (m_thread_sp)
305 return m_thread_sp.get();
306 if (m_process_sp)
307 return m_process_sp.get();
308 return m_target_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309}
Greg Clayton644247c2011-07-07 01:59:51 +0000310
Greg Claytonc14ee322011-09-22 04:58:26 +0000311Target &
312ExecutionContext::GetTargetRef () const
Greg Clayton644247c2011-07-07 01:59:51 +0000313{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000314#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000315 assert (m_target_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000316#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000317 return *m_target_sp;
Greg Clayton644247c2011-07-07 01:59:51 +0000318}
Greg Claytonc14ee322011-09-22 04:58:26 +0000319
320Process &
321ExecutionContext::GetProcessRef () const
322{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000323#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000324 assert (m_process_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000325#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000326 return *m_process_sp;
327}
328
329Thread &
330ExecutionContext::GetThreadRef () const
331{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000332#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000333 assert (m_thread_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000334#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000335 return *m_thread_sp;
336}
337
Jason Molendab57e4a12013-11-04 09:33:30 +0000338StackFrame &
Greg Claytonc14ee322011-09-22 04:58:26 +0000339ExecutionContext::GetFrameRef () const
340{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000341#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000342 assert (m_frame_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000343#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000344 return *m_frame_sp;
345}
346
347void
348ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
349{
350 m_target_sp = target_sp;
351}
352
353void
354ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
355{
356 m_process_sp = process_sp;
357}
358
359void
360ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
361{
362 m_thread_sp = thread_sp;
363}
364
365void
Jason Molendab57e4a12013-11-04 09:33:30 +0000366ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +0000367{
368 m_frame_sp = frame_sp;
369}
370
371void
372ExecutionContext::SetTargetPtr (Target* target)
373{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000374 if (target)
375 m_target_sp = target->shared_from_this();
376 else
377 m_target_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000378}
379
380void
381ExecutionContext::SetProcessPtr (Process *process)
382{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000383 if (process)
384 m_process_sp = process->shared_from_this();
385 else
386 m_process_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000387}
388
389void
390ExecutionContext::SetThreadPtr (Thread *thread)
391{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000392 if (thread)
393 m_thread_sp = thread->shared_from_this();
394 else
395 m_thread_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000396}
397
398void
Jason Molendab57e4a12013-11-04 09:33:30 +0000399ExecutionContext::SetFramePtr (StackFrame *frame)
Greg Claytonc14ee322011-09-22 04:58:26 +0000400{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000401 if (frame)
402 m_frame_sp = frame->shared_from_this();
403 else
404 m_frame_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000405}
406
Greg Claytoncc4d0142012-02-17 07:49:44 +0000407void
408ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
409{
410 m_target_sp = target_sp;
411 if (get_process && target_sp)
412 m_process_sp = target_sp->GetProcessSP();
413 else
414 m_process_sp.reset();
415 m_thread_sp.reset();
416 m_frame_sp.reset();
417}
418
419void
420ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
421{
422 m_process_sp = process_sp;
423 if (process_sp)
424 m_target_sp = process_sp->GetTarget().shared_from_this();
425 else
426 m_target_sp.reset();
427 m_thread_sp.reset();
428 m_frame_sp.reset();
429}
430
431void
432ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
433{
434 m_frame_sp.reset();
435 m_thread_sp = thread_sp;
436 if (thread_sp)
437 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000438 m_process_sp = thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000439 if (m_process_sp)
440 m_target_sp = m_process_sp->GetTarget().shared_from_this();
441 else
442 m_target_sp.reset();
443 }
444 else
445 {
446 m_target_sp.reset();
447 m_process_sp.reset();
448 }
449}
450
451void
Jason Molendab57e4a12013-11-04 09:33:30 +0000452ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000453{
454 m_frame_sp = frame_sp;
455 if (frame_sp)
456 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000457 m_thread_sp = frame_sp->CalculateThread();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000458 if (m_thread_sp)
459 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000460 m_process_sp = m_thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000461 if (m_process_sp)
462 m_target_sp = m_process_sp->GetTarget().shared_from_this();
463 else
464 m_target_sp.reset();
465 }
466 else
467 {
468 m_target_sp.reset();
469 m_process_sp.reset();
470 }
471 }
472 else
473 {
474 m_target_sp.reset();
475 m_process_sp.reset();
476 m_thread_sp.reset();
477 }
478}
479
480ExecutionContext &
481ExecutionContext::operator =(const ExecutionContext &rhs)
482{
483 if (this != &rhs)
484 {
485 m_target_sp = rhs.m_target_sp;
486 m_process_sp = rhs.m_process_sp;
487 m_thread_sp = rhs.m_thread_sp;
488 m_frame_sp = rhs.m_frame_sp;
489 }
490 return *this;
491}
492
493bool
494ExecutionContext::operator ==(const ExecutionContext &rhs) const
495{
496 // Check that the frame shared pointers match, or both are valid and their stack
497 // IDs match since sometimes we get new objects that represent the same
498 // frame within a thread.
499 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
500 {
501 // Check that the thread shared pointers match, or both are valid and
502 // their thread IDs match since sometimes we get new objects that
503 // represent the same thread within a process.
504 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
505 {
506 // Processes and targets don't change much
507 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
508 }
509 }
510 return false;
511}
512
513bool
514ExecutionContext::operator !=(const ExecutionContext &rhs) const
515{
516 return !(*this == rhs);
517}
518
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000519bool
520ExecutionContext::HasTargetScope () const
521{
522 return ((bool) m_target_sp
523 && m_target_sp->IsValid());
524}
525
526bool
527ExecutionContext::HasProcessScope () const
528{
529 return (HasTargetScope()
530 && ((bool) m_process_sp && m_process_sp->IsValid()));
531}
532
533bool
534ExecutionContext::HasThreadScope () const
535{
536 return (HasProcessScope()
537 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
538}
539
540bool
541ExecutionContext::HasFrameScope () const
542{
543 return HasThreadScope() && m_frame_sp;
544}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000545
546ExecutionContextRef::ExecutionContextRef() :
547 m_target_wp (),
548 m_process_wp (),
549 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000550 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000551 m_stack_id ()
552{
553}
554
555ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
556 m_target_wp (),
557 m_process_wp (),
558 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000559 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000560 m_stack_id ()
561{
562 if (exe_ctx)
563 *this = *exe_ctx;
564}
565
566ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
567 m_target_wp (),
568 m_process_wp (),
569 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000570 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000571 m_stack_id ()
572{
573 *this = exe_ctx;
574}
575
Greg Claytoncc4d0142012-02-17 07:49:44 +0000576ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
577 m_target_wp(),
578 m_process_wp(),
579 m_thread_wp(),
Greg Clayton6482d232013-03-15 23:54:07 +0000580 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000581 m_stack_id ()
582{
583 SetTargetPtr (target, adopt_selected);
584}
585
Greg Claytoncc4d0142012-02-17 07:49:44 +0000586ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
587 m_target_wp (rhs.m_target_wp),
588 m_process_wp(rhs.m_process_wp),
589 m_thread_wp (rhs.m_thread_wp),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000590 m_tid (rhs.m_tid),
591 m_stack_id (rhs.m_stack_id)
592{
593}
594
595ExecutionContextRef &
596ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
597{
598 if (this != &rhs)
599 {
600 m_target_wp = rhs.m_target_wp;
601 m_process_wp = rhs.m_process_wp;
602 m_thread_wp = rhs.m_thread_wp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000603 m_tid = rhs.m_tid;
604 m_stack_id = rhs.m_stack_id;
605 }
606 return *this;
607}
608
609ExecutionContextRef &
610ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
611{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000612 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000613 m_process_wp = exe_ctx.GetProcessSP();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000614 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
615 m_thread_wp = thread_sp;
616 if (thread_sp)
617 m_tid = thread_sp->GetID();
618 else
619 m_tid = LLDB_INVALID_THREAD_ID;
Jason Molendab57e4a12013-11-04 09:33:30 +0000620 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000621 if (frame_sp)
622 m_stack_id = frame_sp->GetStackID();
623 else
624 m_stack_id.Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000625 return *this;
626}
627
628void
629ExecutionContextRef::Clear()
630{
631 m_target_wp.reset();
632 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000633 ClearThread();
634 ClearFrame();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000635}
636
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000637ExecutionContextRef::~ExecutionContextRef() = default;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000638
639void
640ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
641{
642 m_target_wp = target_sp;
643}
644
645void
646ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
647{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000648 if (process_sp)
649 {
650 m_process_wp = process_sp;
651 SetTargetSP (process_sp->GetTarget().shared_from_this());
652 }
653 else
654 {
655 m_process_wp.reset();
656 m_target_wp.reset();
657 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000658}
659
660void
661ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
662{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000663 if (thread_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000664 {
665 m_thread_wp = thread_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000666 m_tid = thread_sp->GetID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000667 SetProcessSP (thread_sp->GetProcess());
668 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000669 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000670 {
671 ClearThread();
672 m_process_wp.reset();
673 m_target_wp.reset();
674 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000675}
676
677void
Jason Molendab57e4a12013-11-04 09:33:30 +0000678ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000679{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000680 if (frame_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000681 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000682 m_stack_id = frame_sp->GetStackID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000683 SetThreadSP (frame_sp->GetThread());
684 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000685 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000686 {
687 ClearFrame();
688 ClearThread();
689 m_process_wp.reset();
690 m_target_wp.reset();
691 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000692}
693
694void
695ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
696{
697 Clear();
698 if (target)
699 {
700 lldb::TargetSP target_sp (target->shared_from_this());
701 if (target_sp)
702 {
703 m_target_wp = target_sp;
704 if (adopt_selected)
705 {
706 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
707 if (process_sp)
708 {
709 m_process_wp = process_sp;
710 if (process_sp)
711 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000712 // Only fill in the thread and frame if our process is stopped
Jim Inghamd5ac1ab2015-01-19 23:51:51 +0000713 // Don't just check the state, since we might be in the middle of
714 // resuming.
715 Process::StopLocker stop_locker;
716
717 if (stop_locker.TryLock(&process_sp->GetRunLock()) && StateIsStoppedState (process_sp->GetState(), true))
Greg Claytoncc4d0142012-02-17 07:49:44 +0000718 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000719 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
720 if (!thread_sp)
721 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
722
723 if (thread_sp)
724 {
725 SetThreadSP (thread_sp);
Jason Molendab57e4a12013-11-04 09:33:30 +0000726 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
Greg Clayton9b5450f2012-07-30 22:05:39 +0000727 if (!frame_sp)
728 frame_sp = thread_sp->GetStackFrameAtIndex(0);
729 if (frame_sp)
730 SetFrameSP (frame_sp);
731 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000732 }
733 }
734 }
735 }
736 }
737 }
738}
739
740void
741ExecutionContextRef::SetProcessPtr (Process *process)
742{
743 if (process)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000744 {
745 SetProcessSP(process->shared_from_this());
746 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000747 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000748 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000749 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000750 m_target_wp.reset();
751 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000752}
753
754void
755ExecutionContextRef::SetThreadPtr (Thread *thread)
756{
757 if (thread)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000758 {
759 SetThreadSP (thread->shared_from_this());
760 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000761 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000762 {
763 ClearThread();
764 m_process_wp.reset();
765 m_target_wp.reset();
766 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000767}
768
769void
Jason Molendab57e4a12013-11-04 09:33:30 +0000770ExecutionContextRef::SetFramePtr (StackFrame *frame)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000771{
772 if (frame)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000773 SetFrameSP (frame->shared_from_this());
Greg Claytoncc4d0142012-02-17 07:49:44 +0000774 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000775 Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000776}
777
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000778lldb::TargetSP
779ExecutionContextRef::GetTargetSP () const
780{
781 lldb::TargetSP target_sp(m_target_wp.lock());
782 if (target_sp && !target_sp->IsValid())
783 target_sp.reset();
784 return target_sp;
785}
786
787lldb::ProcessSP
788ExecutionContextRef::GetProcessSP () const
789{
790 lldb::ProcessSP process_sp(m_process_wp.lock());
791 if (process_sp && !process_sp->IsValid())
792 process_sp.reset();
793 return process_sp;
794}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000795
796lldb::ThreadSP
797ExecutionContextRef::GetThreadSP () const
798{
799 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000800
Greg Clayton0b88d812012-04-04 20:43:47 +0000801 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000802 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000803 // We check if the thread has been destroyed in cases where clients
804 // might still have shared pointer to a thread, but the thread is
805 // not valid anymore (not part of the process)
806 if (!thread_sp || !thread_sp->IsValid())
Greg Claytoncc4d0142012-02-17 07:49:44 +0000807 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000808 lldb::ProcessSP process_sp(GetProcessSP());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000809 if (process_sp && process_sp->IsValid())
Greg Clayton0b88d812012-04-04 20:43:47 +0000810 {
811 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
812 m_thread_wp = thread_sp;
813 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000814 }
815 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000816
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000817 // 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 +0000818 // but don't return an invalid one.
819
820 if (thread_sp && !thread_sp->IsValid())
821 thread_sp.reset();
822
Greg Claytoncc4d0142012-02-17 07:49:44 +0000823 return thread_sp;
824}
825
Jason Molendab57e4a12013-11-04 09:33:30 +0000826lldb::StackFrameSP
Greg Claytoncc4d0142012-02-17 07:49:44 +0000827ExecutionContextRef::GetFrameSP () const
828{
Greg Clayton7bcb93d2013-05-24 00:58:29 +0000829 if (m_stack_id.IsValid())
830 {
831 lldb::ThreadSP thread_sp (GetThreadSP());
832 if (thread_sp)
833 return thread_sp->GetFrameWithStackID (m_stack_id);
834 }
Jason Molendab57e4a12013-11-04 09:33:30 +0000835 return lldb::StackFrameSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000836}
837
838ExecutionContext
Greg Clayton44d93782014-01-27 23:43:24 +0000839ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
Greg Claytoncc4d0142012-02-17 07:49:44 +0000840{
Greg Clayton44d93782014-01-27 23:43:24 +0000841 return ExecutionContext(this, thread_and_frame_only_if_stopped);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000842}