blob: e03a560bd0635556213ec38b4cfd2a72248f75fb [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 Claytone1cd1be2012-01-29 20:56:30 +0000122 m_target_sp (t->shared_from_this()),
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{
127 if (t && fill_current_process_thread_frame)
128 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000129 m_process_sp = t->GetProcessSP();
130 if (m_process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000132 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
133 if (m_thread_sp)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000134 m_frame_sp = m_thread_sp->GetSelectedFrame();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 }
136 }
137}
138
Jason Molendab57e4a12013-11-04 09:33:30 +0000139ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
Greg Claytone1cd1be2012-01-29 20:56:30 +0000140 m_target_sp (),
141 m_process_sp (process->shared_from_this()),
142 m_thread_sp (thread->shared_from_this()),
143 m_frame_sp (frame->shared_from_this())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000145 if (process)
146 m_target_sp = process->GetTarget().shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Greg Claytoncc4d0142012-02-17 07:49:44 +0000149ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref) :
150 m_target_sp (exe_ctx_ref.GetTargetSP()),
151 m_process_sp (exe_ctx_ref.GetProcessSP()),
152 m_thread_sp (exe_ctx_ref.GetThreadSP()),
153 m_frame_sp (exe_ctx_ref.GetFrameSP())
154{
155}
156
Greg Clayton44d93782014-01-27 23:43:24 +0000157ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, bool thread_and_frame_only_if_stopped) :
Greg Claytoncc4d0142012-02-17 07:49:44 +0000158 m_target_sp (),
159 m_process_sp (),
160 m_thread_sp (),
161 m_frame_sp ()
162{
163 if (exe_ctx_ref_ptr)
164 {
165 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
166 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
Greg Clayton44d93782014-01-27 23:43:24 +0000167 if (!thread_and_frame_only_if_stopped || (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true)))
168 {
169 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
170 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
171 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000172 }
173}
174
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000175ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
176 m_target_sp (),
177 m_process_sp (),
178 m_thread_sp (),
179 m_frame_sp ()
180{
181 if (exe_ctx_ref_ptr)
182 {
183 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
184 if (m_target_sp)
185 {
186 locker.Lock(m_target_sp->GetAPIMutex());
187 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
188 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
189 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
190 }
191 }
192}
193
194ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
195 m_target_sp (exe_ctx_ref.GetTargetSP()),
196 m_process_sp (),
197 m_thread_sp (),
198 m_frame_sp ()
199{
200 if (m_target_sp)
201 {
202 locker.Lock(m_target_sp->GetAPIMutex());
203 m_process_sp = exe_ctx_ref.GetProcessSP();
204 m_thread_sp = exe_ctx_ref.GetThreadSP();
205 m_frame_sp = exe_ctx_ref.GetFrameSP();
206 }
207}
208
Greg Claytoncc4d0142012-02-17 07:49:44 +0000209ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
210 m_target_sp (),
211 m_process_sp (),
212 m_thread_sp (),
213 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214{
215 if (exe_scope_ptr)
Greg Clayton0603aa92010-10-04 01:05:56 +0000216 exe_scope_ptr->CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
219ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
220{
Greg Clayton0603aa92010-10-04 01:05:56 +0000221 exe_scope_ref.CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
224void
225ExecutionContext::Clear()
226{
Greg Claytonc14ee322011-09-22 04:58:26 +0000227 m_target_sp.reset();
228 m_process_sp.reset();
229 m_thread_sp.reset();
230 m_frame_sp.reset();
231}
232
233ExecutionContext::~ExecutionContext()
234{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
Greg Claytoncc4d0142012-02-17 07:49:44 +0000237uint32_t
238ExecutionContext::GetAddressByteSize() const
239{
240 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
241 m_target_sp->GetArchitecture().GetAddressByteSize();
242 if (m_process_sp)
243 m_process_sp->GetAddressByteSize();
244 return sizeof(void *);
245}
246
Enrico Granata347c2aa2013-10-08 21:49:02 +0000247lldb::ByteOrder
248ExecutionContext::GetByteOrder() const
249{
250 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
251 m_target_sp->GetArchitecture().GetByteOrder();
252 if (m_process_sp)
253 m_process_sp->GetByteOrder();
254 return lldb::endian::InlHostByteOrder();
255}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256
257RegisterContext *
258ExecutionContext::GetRegisterContext () const
259{
Greg Claytonc14ee322011-09-22 04:58:26 +0000260 if (m_frame_sp)
261 return m_frame_sp->GetRegisterContext().get();
262 else if (m_thread_sp)
263 return m_thread_sp->GetRegisterContext().get();
264 return NULL;
265}
266
267Target *
268ExecutionContext::GetTargetPtr () const
269{
270 if (m_target_sp)
271 return m_target_sp.get();
272 if (m_process_sp)
273 return &m_process_sp->GetTarget();
274 return NULL;
275}
276
277Process *
278ExecutionContext::GetProcessPtr () const
279{
280 if (m_process_sp)
281 return m_process_sp.get();
282 if (m_target_sp)
283 return m_target_sp->GetProcessSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 return NULL;
285}
286
287ExecutionContextScope *
288ExecutionContext::GetBestExecutionContextScope () const
289{
Greg Claytonc14ee322011-09-22 04:58:26 +0000290 if (m_frame_sp)
291 return m_frame_sp.get();
292 if (m_thread_sp)
293 return m_thread_sp.get();
294 if (m_process_sp)
295 return m_process_sp.get();
296 return m_target_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297}
Greg Clayton644247c2011-07-07 01:59:51 +0000298
Greg Claytonc14ee322011-09-22 04:58:26 +0000299Target &
300ExecutionContext::GetTargetRef () const
Greg Clayton644247c2011-07-07 01:59:51 +0000301{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000302#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000303 assert (m_target_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000304#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000305 return *m_target_sp;
Greg Clayton644247c2011-07-07 01:59:51 +0000306}
Greg Claytonc14ee322011-09-22 04:58:26 +0000307
308Process &
309ExecutionContext::GetProcessRef () const
310{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000311#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000312 assert (m_process_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000313#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000314 return *m_process_sp;
315}
316
317Thread &
318ExecutionContext::GetThreadRef () const
319{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000320#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000321 assert (m_thread_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000322#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000323 return *m_thread_sp;
324}
325
Jason Molendab57e4a12013-11-04 09:33:30 +0000326StackFrame &
Greg Claytonc14ee322011-09-22 04:58:26 +0000327ExecutionContext::GetFrameRef () const
328{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000329#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000330 assert (m_frame_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000331#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000332 return *m_frame_sp;
333}
334
335void
336ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
337{
338 m_target_sp = target_sp;
339}
340
341void
342ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
343{
344 m_process_sp = process_sp;
345}
346
347void
348ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
349{
350 m_thread_sp = thread_sp;
351}
352
353void
Jason Molendab57e4a12013-11-04 09:33:30 +0000354ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +0000355{
356 m_frame_sp = frame_sp;
357}
358
359void
360ExecutionContext::SetTargetPtr (Target* target)
361{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000362 if (target)
363 m_target_sp = target->shared_from_this();
364 else
365 m_target_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000366}
367
368void
369ExecutionContext::SetProcessPtr (Process *process)
370{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000371 if (process)
372 m_process_sp = process->shared_from_this();
373 else
374 m_process_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000375}
376
377void
378ExecutionContext::SetThreadPtr (Thread *thread)
379{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000380 if (thread)
381 m_thread_sp = thread->shared_from_this();
382 else
383 m_thread_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000384}
385
386void
Jason Molendab57e4a12013-11-04 09:33:30 +0000387ExecutionContext::SetFramePtr (StackFrame *frame)
Greg Claytonc14ee322011-09-22 04:58:26 +0000388{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000389 if (frame)
390 m_frame_sp = frame->shared_from_this();
391 else
392 m_frame_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000393}
394
Greg Claytoncc4d0142012-02-17 07:49:44 +0000395void
396ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
397{
398 m_target_sp = target_sp;
399 if (get_process && target_sp)
400 m_process_sp = target_sp->GetProcessSP();
401 else
402 m_process_sp.reset();
403 m_thread_sp.reset();
404 m_frame_sp.reset();
405}
406
407void
408ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
409{
410 m_process_sp = process_sp;
411 if (process_sp)
412 m_target_sp = process_sp->GetTarget().shared_from_this();
413 else
414 m_target_sp.reset();
415 m_thread_sp.reset();
416 m_frame_sp.reset();
417}
418
419void
420ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
421{
422 m_frame_sp.reset();
423 m_thread_sp = thread_sp;
424 if (thread_sp)
425 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000426 m_process_sp = thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000427 if (m_process_sp)
428 m_target_sp = m_process_sp->GetTarget().shared_from_this();
429 else
430 m_target_sp.reset();
431 }
432 else
433 {
434 m_target_sp.reset();
435 m_process_sp.reset();
436 }
437}
438
439void
Jason Molendab57e4a12013-11-04 09:33:30 +0000440ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000441{
442 m_frame_sp = frame_sp;
443 if (frame_sp)
444 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000445 m_thread_sp = frame_sp->CalculateThread();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000446 if (m_thread_sp)
447 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000448 m_process_sp = m_thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000449 if (m_process_sp)
450 m_target_sp = m_process_sp->GetTarget().shared_from_this();
451 else
452 m_target_sp.reset();
453 }
454 else
455 {
456 m_target_sp.reset();
457 m_process_sp.reset();
458 }
459 }
460 else
461 {
462 m_target_sp.reset();
463 m_process_sp.reset();
464 m_thread_sp.reset();
465 }
466}
467
468ExecutionContext &
469ExecutionContext::operator =(const ExecutionContext &rhs)
470{
471 if (this != &rhs)
472 {
473 m_target_sp = rhs.m_target_sp;
474 m_process_sp = rhs.m_process_sp;
475 m_thread_sp = rhs.m_thread_sp;
476 m_frame_sp = rhs.m_frame_sp;
477 }
478 return *this;
479}
480
481bool
482ExecutionContext::operator ==(const ExecutionContext &rhs) const
483{
484 // Check that the frame shared pointers match, or both are valid and their stack
485 // IDs match since sometimes we get new objects that represent the same
486 // frame within a thread.
487 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
488 {
489 // Check that the thread shared pointers match, or both are valid and
490 // their thread IDs match since sometimes we get new objects that
491 // represent the same thread within a process.
492 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
493 {
494 // Processes and targets don't change much
495 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
496 }
497 }
498 return false;
499}
500
501bool
502ExecutionContext::operator !=(const ExecutionContext &rhs) const
503{
504 return !(*this == rhs);
505}
506
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000507bool
508ExecutionContext::HasTargetScope () const
509{
510 return ((bool) m_target_sp
511 && m_target_sp->IsValid());
512}
513
514bool
515ExecutionContext::HasProcessScope () const
516{
517 return (HasTargetScope()
518 && ((bool) m_process_sp && m_process_sp->IsValid()));
519}
520
521bool
522ExecutionContext::HasThreadScope () const
523{
524 return (HasProcessScope()
525 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
526}
527
528bool
529ExecutionContext::HasFrameScope () const
530{
531 return HasThreadScope() && m_frame_sp;
532}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000533
534ExecutionContextRef::ExecutionContextRef() :
535 m_target_wp (),
536 m_process_wp (),
537 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000538 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000539 m_stack_id ()
540{
541}
542
543ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
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 if (exe_ctx)
551 *this = *exe_ctx;
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 *this = exe_ctx;
562}
563
564
565ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
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 SetTargetPtr (target, adopt_selected);
573}
574
575
576
577
578ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
579 m_target_wp (rhs.m_target_wp),
580 m_process_wp(rhs.m_process_wp),
581 m_thread_wp (rhs.m_thread_wp),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000582 m_tid (rhs.m_tid),
583 m_stack_id (rhs.m_stack_id)
584{
585}
586
587ExecutionContextRef &
588ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
589{
590 if (this != &rhs)
591 {
592 m_target_wp = rhs.m_target_wp;
593 m_process_wp = rhs.m_process_wp;
594 m_thread_wp = rhs.m_thread_wp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000595 m_tid = rhs.m_tid;
596 m_stack_id = rhs.m_stack_id;
597 }
598 return *this;
599}
600
601ExecutionContextRef &
602ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
603{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000604 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000605 m_process_wp = exe_ctx.GetProcessSP();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000606 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
607 m_thread_wp = thread_sp;
608 if (thread_sp)
609 m_tid = thread_sp->GetID();
610 else
611 m_tid = LLDB_INVALID_THREAD_ID;
Jason Molendab57e4a12013-11-04 09:33:30 +0000612 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000613 if (frame_sp)
614 m_stack_id = frame_sp->GetStackID();
615 else
616 m_stack_id.Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000617 return *this;
618}
619
620void
621ExecutionContextRef::Clear()
622{
623 m_target_wp.reset();
624 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000625 ClearThread();
626 ClearFrame();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000627}
628
629ExecutionContextRef::~ExecutionContextRef()
630{
631}
632
633void
634ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
635{
636 m_target_wp = target_sp;
637}
638
639void
640ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
641{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000642 if (process_sp)
643 {
644 m_process_wp = process_sp;
645 SetTargetSP (process_sp->GetTarget().shared_from_this());
646 }
647 else
648 {
649 m_process_wp.reset();
650 m_target_wp.reset();
651 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000652}
653
654void
655ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
656{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000657 if (thread_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000658 {
659 m_thread_wp = thread_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000660 m_tid = thread_sp->GetID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000661 SetProcessSP (thread_sp->GetProcess());
662 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000663 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000664 {
665 ClearThread();
666 m_process_wp.reset();
667 m_target_wp.reset();
668 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000669}
670
671void
Jason Molendab57e4a12013-11-04 09:33:30 +0000672ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000673{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000674 if (frame_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000675 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000676 m_stack_id = frame_sp->GetStackID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000677 SetThreadSP (frame_sp->GetThread());
678 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000679 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000680 {
681 ClearFrame();
682 ClearThread();
683 m_process_wp.reset();
684 m_target_wp.reset();
685 }
686
Greg Claytoncc4d0142012-02-17 07:49:44 +0000687}
688
689void
690ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
691{
692 Clear();
693 if (target)
694 {
695 lldb::TargetSP target_sp (target->shared_from_this());
696 if (target_sp)
697 {
698 m_target_wp = target_sp;
699 if (adopt_selected)
700 {
701 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
702 if (process_sp)
703 {
704 m_process_wp = process_sp;
705 if (process_sp)
706 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000707 // Only fill in the thread and frame if our process is stopped
Jim Inghamd5ac1ab2015-01-19 23:51:51 +0000708 // Don't just check the state, since we might be in the middle of
709 // resuming.
710 Process::StopLocker stop_locker;
711
712 if (stop_locker.TryLock(&process_sp->GetRunLock()) && StateIsStoppedState (process_sp->GetState(), true))
Greg Claytoncc4d0142012-02-17 07:49:44 +0000713 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000714 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
715 if (!thread_sp)
716 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
717
718 if (thread_sp)
719 {
720 SetThreadSP (thread_sp);
Jason Molendab57e4a12013-11-04 09:33:30 +0000721 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
Greg Clayton9b5450f2012-07-30 22:05:39 +0000722 if (!frame_sp)
723 frame_sp = thread_sp->GetStackFrameAtIndex(0);
724 if (frame_sp)
725 SetFrameSP (frame_sp);
726 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000727 }
728 }
729 }
730 }
731 }
732 }
733}
734
735void
736ExecutionContextRef::SetProcessPtr (Process *process)
737{
738 if (process)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000739 {
740 SetProcessSP(process->shared_from_this());
741 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000742 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000743 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000744 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000745 m_target_wp.reset();
746 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000747}
748
749void
750ExecutionContextRef::SetThreadPtr (Thread *thread)
751{
752 if (thread)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000753 {
754 SetThreadSP (thread->shared_from_this());
755 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000756 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000757 {
758 ClearThread();
759 m_process_wp.reset();
760 m_target_wp.reset();
761 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000762}
763
764void
Jason Molendab57e4a12013-11-04 09:33:30 +0000765ExecutionContextRef::SetFramePtr (StackFrame *frame)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000766{
767 if (frame)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000768 SetFrameSP (frame->shared_from_this());
Greg Claytoncc4d0142012-02-17 07:49:44 +0000769 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000770 Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000771}
772
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000773lldb::TargetSP
774ExecutionContextRef::GetTargetSP () const
775{
776 lldb::TargetSP target_sp(m_target_wp.lock());
777 if (target_sp && !target_sp->IsValid())
778 target_sp.reset();
779 return target_sp;
780}
781
782lldb::ProcessSP
783ExecutionContextRef::GetProcessSP () const
784{
785 lldb::ProcessSP process_sp(m_process_wp.lock());
786 if (process_sp && !process_sp->IsValid())
787 process_sp.reset();
788 return process_sp;
789}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000790
791lldb::ThreadSP
792ExecutionContextRef::GetThreadSP () const
793{
794 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000795
Greg Clayton0b88d812012-04-04 20:43:47 +0000796 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000797 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000798 // We check if the thread has been destroyed in cases where clients
799 // might still have shared pointer to a thread, but the thread is
800 // not valid anymore (not part of the process)
801 if (!thread_sp || !thread_sp->IsValid())
Greg Claytoncc4d0142012-02-17 07:49:44 +0000802 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000803 lldb::ProcessSP process_sp(GetProcessSP());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000804 if (process_sp && process_sp->IsValid())
Greg Clayton0b88d812012-04-04 20:43:47 +0000805 {
806 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
807 m_thread_wp = thread_sp;
808 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000809 }
810 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000811
812 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
813 // but don't return an invalid one.
814
815 if (thread_sp && !thread_sp->IsValid())
816 thread_sp.reset();
817
Greg Claytoncc4d0142012-02-17 07:49:44 +0000818 return thread_sp;
819}
820
Jason Molendab57e4a12013-11-04 09:33:30 +0000821lldb::StackFrameSP
Greg Claytoncc4d0142012-02-17 07:49:44 +0000822ExecutionContextRef::GetFrameSP () const
823{
Greg Clayton7bcb93d2013-05-24 00:58:29 +0000824 if (m_stack_id.IsValid())
825 {
826 lldb::ThreadSP thread_sp (GetThreadSP());
827 if (thread_sp)
828 return thread_sp->GetFrameWithStackID (m_stack_id);
829 }
Jason Molendab57e4a12013-11-04 09:33:30 +0000830 return lldb::StackFrameSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000831}
832
833ExecutionContext
Greg Clayton44d93782014-01-27 23:43:24 +0000834ExecutionContextRef::Lock (bool thread_and_frame_only_if_stopped) const
Greg Claytoncc4d0142012-02-17 07:49:44 +0000835{
Greg Clayton44d93782014-01-27 23:43:24 +0000836 return ExecutionContext(this, thread_and_frame_only_if_stopped);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000837}
838
839