blob: ff584361c2963b2aa60c608ab2d5f6957ded1614 [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
10#include "lldb/Target/ExecutionContext.h"
Greg Clayton9b5450f2012-07-30 22:05:39 +000011
12#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Target/ExecutionContextScope.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000014#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
18
19using namespace lldb_private;
20
21ExecutionContext::ExecutionContext() :
Greg Claytonc14ee322011-09-22 04:58:26 +000022 m_target_sp (),
23 m_process_sp (),
24 m_thread_sp (),
25 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026{
27}
28
Greg Claytonc14ee322011-09-22 04:58:26 +000029ExecutionContext::ExecutionContext (const ExecutionContext &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +000030 m_target_sp(rhs.m_target_sp),
Greg Claytonc14ee322011-09-22 04:58:26 +000031 m_process_sp(rhs.m_process_sp),
Greg Claytoncc4d0142012-02-17 07:49:44 +000032 m_thread_sp(rhs.m_thread_sp),
33 m_frame_sp(rhs.m_frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +000034{
35}
36
Greg Claytoncc4d0142012-02-17 07:49:44 +000037ExecutionContext::ExecutionContext (const lldb::TargetSP &target_sp, bool get_process) :
38 m_target_sp (),
39 m_process_sp (),
40 m_thread_sp (),
41 m_frame_sp ()
Greg Claytonc14ee322011-09-22 04:58:26 +000042{
Greg Claytoncc4d0142012-02-17 07:49:44 +000043 if (target_sp)
44 SetContext (target_sp, get_process);
45}
46
47ExecutionContext::ExecutionContext (const lldb::ProcessSP &process_sp) :
48 m_target_sp (),
49 m_process_sp (),
50 m_thread_sp (),
51 m_frame_sp ()
52{
53 if (process_sp)
54 SetContext (process_sp);
55}
56
57ExecutionContext::ExecutionContext (const lldb::ThreadSP &thread_sp) :
58 m_target_sp (),
59 m_process_sp (),
60 m_thread_sp (),
61 m_frame_sp ()
62{
63 if (thread_sp)
64 SetContext (thread_sp);
65}
66
Jason Molendab57e4a12013-11-04 09:33:30 +000067ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
Greg Claytoncc4d0142012-02-17 07:49:44 +000068 m_target_sp (),
69 m_process_sp (),
70 m_thread_sp (),
71 m_frame_sp ()
72{
73 if (frame_sp)
74 SetContext (frame_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +000075}
76
Greg Clayton1ac04c32012-02-21 00:09:25 +000077ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
78 m_target_sp (),
79 m_process_sp (),
80 m_thread_sp (),
81 m_frame_sp ()
82{
83 lldb::TargetSP target_sp(target_wp.lock());
84 if (target_sp)
85 SetContext (target_sp, get_process);
86}
87
88ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
89 m_target_sp (),
90 m_process_sp (),
91 m_thread_sp (),
92 m_frame_sp ()
93{
94 lldb::ProcessSP process_sp(process_wp.lock());
95 if (process_sp)
96 SetContext (process_sp);
97}
98
99ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
100 m_target_sp (),
101 m_process_sp (),
102 m_thread_sp (),
103 m_frame_sp ()
104{
105 lldb::ThreadSP thread_sp(thread_wp.lock());
106 if (thread_sp)
107 SetContext (thread_sp);
108}
109
Jason Molendab57e4a12013-11-04 09:33:30 +0000110ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
Greg Clayton1ac04c32012-02-21 00:09:25 +0000111 m_target_sp (),
112 m_process_sp (),
113 m_thread_sp (),
114 m_frame_sp ()
115{
Jason Molendab57e4a12013-11-04 09:33:30 +0000116 lldb::StackFrameSP frame_sp(frame_wp.lock());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000117 if (frame_sp)
118 SetContext (frame_sp);
119}
120
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
Greg Clayton526ae042015-02-12 00:34:25 +0000122 m_target_sp (),
Greg Claytonc14ee322011-09-22 04:58:26 +0000123 m_process_sp (),
124 m_thread_sp (),
125 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126{
Greg Clayton526ae042015-02-12 00:34:25 +0000127 if (t)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 {
Greg Clayton526ae042015-02-12 00:34:25 +0000129 m_target_sp = t->shared_from_this();
130 if (fill_current_process_thread_frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 {
Greg Clayton526ae042015-02-12 00:34:25 +0000132 m_process_sp = t->GetProcessSP();
133 if (m_process_sp)
134 {
135 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
136 if (m_thread_sp)
137 m_frame_sp = m_thread_sp->GetSelectedFrame();
138 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 }
140 }
141}
142
Jason Molendab57e4a12013-11-04 09:33:30 +0000143ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
Greg Claytone1cd1be2012-01-29 20:56:30 +0000144 m_target_sp (),
Greg Clayton526ae042015-02-12 00:34:25 +0000145 m_process_sp (),
146 m_thread_sp (),
147 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000149 if (process)
Greg Clayton526ae042015-02-12 00:34:25 +0000150 {
151 m_process_sp = process->shared_from_this();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000152 m_target_sp = process->GetTarget().shared_from_this();
Greg Clayton526ae042015-02-12 00:34:25 +0000153 }
154 if (thread)
155 m_thread_sp = thread->shared_from_this();
156 if (frame)
157 m_frame_sp = frame->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Greg Claytoncc4d0142012-02-17 07:49:44 +0000160ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref) :
161 m_target_sp (exe_ctx_ref.GetTargetSP()),
162 m_process_sp (exe_ctx_ref.GetProcessSP()),
163 m_thread_sp (exe_ctx_ref.GetThreadSP()),
164 m_frame_sp (exe_ctx_ref.GetFrameSP())
165{
166}
167
Greg Clayton44d93782014-01-27 23:43:24 +0000168ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, bool thread_and_frame_only_if_stopped) :
Greg Claytoncc4d0142012-02-17 07:49:44 +0000169 m_target_sp (),
170 m_process_sp (),
171 m_thread_sp (),
172 m_frame_sp ()
173{
174 if (exe_ctx_ref_ptr)
175 {
176 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
177 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
Greg Clayton44d93782014-01-27 23:43:24 +0000178 if (!thread_and_frame_only_if_stopped || (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true)))
179 {
180 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
181 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
182 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000183 }
184}
185
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000186ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
187 m_target_sp (),
188 m_process_sp (),
189 m_thread_sp (),
190 m_frame_sp ()
191{
192 if (exe_ctx_ref_ptr)
193 {
194 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
195 if (m_target_sp)
196 {
197 locker.Lock(m_target_sp->GetAPIMutex());
198 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
199 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
200 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
201 }
202 }
203}
204
205ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
206 m_target_sp (exe_ctx_ref.GetTargetSP()),
207 m_process_sp (),
208 m_thread_sp (),
209 m_frame_sp ()
210{
211 if (m_target_sp)
212 {
213 locker.Lock(m_target_sp->GetAPIMutex());
214 m_process_sp = exe_ctx_ref.GetProcessSP();
215 m_thread_sp = exe_ctx_ref.GetThreadSP();
216 m_frame_sp = exe_ctx_ref.GetFrameSP();
217 }
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
244ExecutionContext::~ExecutionContext()
245{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
Greg Claytoncc4d0142012-02-17 07:49:44 +0000248uint32_t
249ExecutionContext::GetAddressByteSize() const
250{
251 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
Hafiz Abid Qadeered69e302015-02-25 16:01:12 +0000252 return m_target_sp->GetArchitecture().GetAddressByteSize();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000253 if (m_process_sp)
Hafiz Abid Qadeered69e302015-02-25 16:01:12 +0000254 return m_process_sp->GetAddressByteSize();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000255 return sizeof(void *);
256}
257
Enrico Granata347c2aa2013-10-08 21:49:02 +0000258lldb::ByteOrder
259ExecutionContext::GetByteOrder() const
260{
261 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
262 m_target_sp->GetArchitecture().GetByteOrder();
263 if (m_process_sp)
264 m_process_sp->GetByteOrder();
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000265 return endian::InlHostByteOrder();
Enrico Granata347c2aa2013-10-08 21:49:02 +0000266}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
268RegisterContext *
269ExecutionContext::GetRegisterContext () const
270{
Greg Claytonc14ee322011-09-22 04:58:26 +0000271 if (m_frame_sp)
272 return m_frame_sp->GetRegisterContext().get();
273 else if (m_thread_sp)
274 return m_thread_sp->GetRegisterContext().get();
275 return NULL;
276}
277
278Target *
279ExecutionContext::GetTargetPtr () const
280{
281 if (m_target_sp)
282 return m_target_sp.get();
283 if (m_process_sp)
284 return &m_process_sp->GetTarget();
285 return NULL;
286}
287
288Process *
289ExecutionContext::GetProcessPtr () const
290{
291 if (m_process_sp)
292 return m_process_sp.get();
293 if (m_target_sp)
294 return m_target_sp->GetProcessSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 return NULL;
296}
297
298ExecutionContextScope *
299ExecutionContext::GetBestExecutionContextScope () const
300{
Greg Claytonc14ee322011-09-22 04:58:26 +0000301 if (m_frame_sp)
302 return m_frame_sp.get();
303 if (m_thread_sp)
304 return m_thread_sp.get();
305 if (m_process_sp)
306 return m_process_sp.get();
307 return m_target_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308}
Greg Clayton644247c2011-07-07 01:59:51 +0000309
Greg Claytonc14ee322011-09-22 04:58:26 +0000310Target &
311ExecutionContext::GetTargetRef () const
Greg Clayton644247c2011-07-07 01:59:51 +0000312{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000313#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000314 assert (m_target_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000315#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000316 return *m_target_sp;
Greg Clayton644247c2011-07-07 01:59:51 +0000317}
Greg Claytonc14ee322011-09-22 04:58:26 +0000318
319Process &
320ExecutionContext::GetProcessRef () const
321{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000322#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000323 assert (m_process_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000324#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000325 return *m_process_sp;
326}
327
328Thread &
329ExecutionContext::GetThreadRef () const
330{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000331#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000332 assert (m_thread_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000333#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000334 return *m_thread_sp;
335}
336
Jason Molendab57e4a12013-11-04 09:33:30 +0000337StackFrame &
Greg Claytonc14ee322011-09-22 04:58:26 +0000338ExecutionContext::GetFrameRef () const
339{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000340#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000341 assert (m_frame_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000342#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000343 return *m_frame_sp;
344}
345
346void
347ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
348{
349 m_target_sp = target_sp;
350}
351
352void
353ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
354{
355 m_process_sp = process_sp;
356}
357
358void
359ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
360{
361 m_thread_sp = thread_sp;
362}
363
364void
Jason Molendab57e4a12013-11-04 09:33:30 +0000365ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +0000366{
367 m_frame_sp = frame_sp;
368}
369
370void
371ExecutionContext::SetTargetPtr (Target* target)
372{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000373 if (target)
374 m_target_sp = target->shared_from_this();
375 else
376 m_target_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000377}
378
379void
380ExecutionContext::SetProcessPtr (Process *process)
381{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000382 if (process)
383 m_process_sp = process->shared_from_this();
384 else
385 m_process_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000386}
387
388void
389ExecutionContext::SetThreadPtr (Thread *thread)
390{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000391 if (thread)
392 m_thread_sp = thread->shared_from_this();
393 else
394 m_thread_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000395}
396
397void
Jason Molendab57e4a12013-11-04 09:33:30 +0000398ExecutionContext::SetFramePtr (StackFrame *frame)
Greg Claytonc14ee322011-09-22 04:58:26 +0000399{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000400 if (frame)
401 m_frame_sp = frame->shared_from_this();
402 else
403 m_frame_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000404}
405
Greg Claytoncc4d0142012-02-17 07:49:44 +0000406void
407ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
408{
409 m_target_sp = target_sp;
410 if (get_process && target_sp)
411 m_process_sp = target_sp->GetProcessSP();
412 else
413 m_process_sp.reset();
414 m_thread_sp.reset();
415 m_frame_sp.reset();
416}
417
418void
419ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
420{
421 m_process_sp = process_sp;
422 if (process_sp)
423 m_target_sp = process_sp->GetTarget().shared_from_this();
424 else
425 m_target_sp.reset();
426 m_thread_sp.reset();
427 m_frame_sp.reset();
428}
429
430void
431ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
432{
433 m_frame_sp.reset();
434 m_thread_sp = thread_sp;
435 if (thread_sp)
436 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000437 m_process_sp = thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000438 if (m_process_sp)
439 m_target_sp = m_process_sp->GetTarget().shared_from_this();
440 else
441 m_target_sp.reset();
442 }
443 else
444 {
445 m_target_sp.reset();
446 m_process_sp.reset();
447 }
448}
449
450void
Jason Molendab57e4a12013-11-04 09:33:30 +0000451ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000452{
453 m_frame_sp = frame_sp;
454 if (frame_sp)
455 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000456 m_thread_sp = frame_sp->CalculateThread();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000457 if (m_thread_sp)
458 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000459 m_process_sp = m_thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000460 if (m_process_sp)
461 m_target_sp = m_process_sp->GetTarget().shared_from_this();
462 else
463 m_target_sp.reset();
464 }
465 else
466 {
467 m_target_sp.reset();
468 m_process_sp.reset();
469 }
470 }
471 else
472 {
473 m_target_sp.reset();
474 m_process_sp.reset();
475 m_thread_sp.reset();
476 }
477}
478
479ExecutionContext &
480ExecutionContext::operator =(const ExecutionContext &rhs)
481{
482 if (this != &rhs)
483 {
484 m_target_sp = rhs.m_target_sp;
485 m_process_sp = rhs.m_process_sp;
486 m_thread_sp = rhs.m_thread_sp;
487 m_frame_sp = rhs.m_frame_sp;
488 }
489 return *this;
490}
491
492bool
493ExecutionContext::operator ==(const ExecutionContext &rhs) const
494{
495 // Check that the frame shared pointers match, or both are valid and their stack
496 // IDs match since sometimes we get new objects that represent the same
497 // frame within a thread.
498 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
499 {
500 // Check that the thread shared pointers match, or both are valid and
501 // their thread IDs match since sometimes we get new objects that
502 // represent the same thread within a process.
503 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
504 {
505 // Processes and targets don't change much
506 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
507 }
508 }
509 return false;
510}
511
512bool
513ExecutionContext::operator !=(const ExecutionContext &rhs) const
514{
515 return !(*this == rhs);
516}
517
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000518bool
519ExecutionContext::HasTargetScope () const
520{
521 return ((bool) m_target_sp
522 && m_target_sp->IsValid());
523}
524
525bool
526ExecutionContext::HasProcessScope () const
527{
528 return (HasTargetScope()
529 && ((bool) m_process_sp && m_process_sp->IsValid()));
530}
531
532bool
533ExecutionContext::HasThreadScope () const
534{
535 return (HasProcessScope()
536 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
537}
538
539bool
540ExecutionContext::HasFrameScope () const
541{
542 return HasThreadScope() && m_frame_sp;
543}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000544
545ExecutionContextRef::ExecutionContextRef() :
546 m_target_wp (),
547 m_process_wp (),
548 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000549 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000550 m_stack_id ()
551{
552}
553
554ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
555 m_target_wp (),
556 m_process_wp (),
557 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000558 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000559 m_stack_id ()
560{
561 if (exe_ctx)
562 *this = *exe_ctx;
563}
564
565ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
566 m_target_wp (),
567 m_process_wp (),
568 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000569 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000570 m_stack_id ()
571{
572 *this = exe_ctx;
573}
574
575
576ExecutionContextRef::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
586
587
588
589ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
590 m_target_wp (rhs.m_target_wp),
591 m_process_wp(rhs.m_process_wp),
592 m_thread_wp (rhs.m_thread_wp),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000593 m_tid (rhs.m_tid),
594 m_stack_id (rhs.m_stack_id)
595{
596}
597
598ExecutionContextRef &
599ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
600{
601 if (this != &rhs)
602 {
603 m_target_wp = rhs.m_target_wp;
604 m_process_wp = rhs.m_process_wp;
605 m_thread_wp = rhs.m_thread_wp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000606 m_tid = rhs.m_tid;
607 m_stack_id = rhs.m_stack_id;
608 }
609 return *this;
610}
611
612ExecutionContextRef &
613ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
614{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000615 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000616 m_process_wp = exe_ctx.GetProcessSP();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000617 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
618 m_thread_wp = thread_sp;
619 if (thread_sp)
620 m_tid = thread_sp->GetID();
621 else
622 m_tid = LLDB_INVALID_THREAD_ID;
Jason Molendab57e4a12013-11-04 09:33:30 +0000623 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000624 if (frame_sp)
625 m_stack_id = frame_sp->GetStackID();
626 else
627 m_stack_id.Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000628 return *this;
629}
630
631void
632ExecutionContextRef::Clear()
633{
634 m_target_wp.reset();
635 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000636 ClearThread();
637 ClearFrame();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000638}
639
640ExecutionContextRef::~ExecutionContextRef()
641{
642}
643
644void
645ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
646{
647 m_target_wp = target_sp;
648}
649
650void
651ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
652{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000653 if (process_sp)
654 {
655 m_process_wp = process_sp;
656 SetTargetSP (process_sp->GetTarget().shared_from_this());
657 }
658 else
659 {
660 m_process_wp.reset();
661 m_target_wp.reset();
662 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000663}
664
665void
666ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
667{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000668 if (thread_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000669 {
670 m_thread_wp = thread_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000671 m_tid = thread_sp->GetID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000672 SetProcessSP (thread_sp->GetProcess());
673 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000674 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000675 {
676 ClearThread();
677 m_process_wp.reset();
678 m_target_wp.reset();
679 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000680}
681
682void
Jason Molendab57e4a12013-11-04 09:33:30 +0000683ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000684{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000685 if (frame_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000686 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000687 m_stack_id = frame_sp->GetStackID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000688 SetThreadSP (frame_sp->GetThread());
689 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000690 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000691 {
692 ClearFrame();
693 ClearThread();
694 m_process_wp.reset();
695 m_target_wp.reset();
696 }
697
Greg Claytoncc4d0142012-02-17 07:49:44 +0000698}
699
700void
701ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
702{
703 Clear();
704 if (target)
705 {
706 lldb::TargetSP target_sp (target->shared_from_this());
707 if (target_sp)
708 {
709 m_target_wp = target_sp;
710 if (adopt_selected)
711 {
712 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
713 if (process_sp)
714 {
715 m_process_wp = process_sp;
716 if (process_sp)
717 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000718 // Only fill in the thread and frame if our process is stopped
Jim Inghamd5ac1ab2015-01-19 23:51:51 +0000719 // Don't just check the state, since we might be in the middle of
720 // resuming.
721 Process::StopLocker stop_locker;
722
723 if (stop_locker.TryLock(&process_sp->GetRunLock()) && StateIsStoppedState (process_sp->GetState(), true))
Greg Claytoncc4d0142012-02-17 07:49:44 +0000724 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000725 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
726 if (!thread_sp)
727 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
728
729 if (thread_sp)
730 {
731 SetThreadSP (thread_sp);
Jason Molendab57e4a12013-11-04 09:33:30 +0000732 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
Greg Clayton9b5450f2012-07-30 22:05:39 +0000733 if (!frame_sp)
734 frame_sp = thread_sp->GetStackFrameAtIndex(0);
735 if (frame_sp)
736 SetFrameSP (frame_sp);
737 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000738 }
739 }
740 }
741 }
742 }
743 }
744}
745
746void
747ExecutionContextRef::SetProcessPtr (Process *process)
748{
749 if (process)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000750 {
751 SetProcessSP(process->shared_from_this());
752 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000753 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000754 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000755 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000756 m_target_wp.reset();
757 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000758}
759
760void
761ExecutionContextRef::SetThreadPtr (Thread *thread)
762{
763 if (thread)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000764 {
765 SetThreadSP (thread->shared_from_this());
766 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000767 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000768 {
769 ClearThread();
770 m_process_wp.reset();
771 m_target_wp.reset();
772 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000773}
774
775void
Jason Molendab57e4a12013-11-04 09:33:30 +0000776ExecutionContextRef::SetFramePtr (StackFrame *frame)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000777{
778 if (frame)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000779 SetFrameSP (frame->shared_from_this());
Greg Claytoncc4d0142012-02-17 07:49:44 +0000780 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000781 Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000782}
783
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000784lldb::TargetSP
785ExecutionContextRef::GetTargetSP () const
786{
787 lldb::TargetSP target_sp(m_target_wp.lock());
788 if (target_sp && !target_sp->IsValid())
789 target_sp.reset();
790 return target_sp;
791}
792
793lldb::ProcessSP
794ExecutionContextRef::GetProcessSP () const
795{
796 lldb::ProcessSP process_sp(m_process_wp.lock());
797 if (process_sp && !process_sp->IsValid())
798 process_sp.reset();
799 return process_sp;
800}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000801
802lldb::ThreadSP
803ExecutionContextRef::GetThreadSP () const
804{
805 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000806
Greg Clayton0b88d812012-04-04 20:43:47 +0000807 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000808 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000809 // We check if the thread has been destroyed in cases where clients
810 // might still have shared pointer to a thread, but the thread is
811 // not valid anymore (not part of the process)
812 if (!thread_sp || !thread_sp->IsValid())
Greg Claytoncc4d0142012-02-17 07:49:44 +0000813 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000814 lldb::ProcessSP process_sp(GetProcessSP());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000815 if (process_sp && process_sp->IsValid())
Greg Clayton0b88d812012-04-04 20:43:47 +0000816 {
817 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
818 m_thread_wp = thread_sp;
819 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000820 }
821 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000822
823 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
824 // but don't return an invalid one.
825
826 if (thread_sp && !thread_sp->IsValid())
827 thread_sp.reset();
828
Greg Claytoncc4d0142012-02-17 07:49:44 +0000829 return thread_sp;
830}
831
Jason Molendab57e4a12013-11-04 09:33:30 +0000832lldb::StackFrameSP
Greg Claytoncc4d0142012-02-17 07:49:44 +0000833ExecutionContextRef::GetFrameSP () const
834{
Greg Clayton7bcb93d2013-05-24 00:58:29 +0000835 if (m_stack_id.IsValid())
836 {
837 lldb::ThreadSP thread_sp (GetThreadSP());
838 if (thread_sp)
839 return thread_sp->GetFrameWithStackID (m_stack_id);
840 }
Jason Molendab57e4a12013-11-04 09:33:30 +0000841 return lldb::StackFrameSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000842}
843
844ExecutionContext
Greg Clayton44d93782014-01-27 23:43:24 +0000845ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
Greg Claytoncc4d0142012-02-17 07:49:44 +0000846{
Greg Clayton44d93782014-01-27 23:43:24 +0000847 return ExecutionContext(this, thread_and_frame_only_if_stopped);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000848}
849
850