blob: 9ace4c93b17f429231ecd99639a6089c2fd46191 [file] [log] [blame]
Chris Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +00009
10#include "lldb/Target/ExecutionContext.h"
Greg Claytond8ae7f22012-07-30 22:05:39 +000011
12#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Target/ExecutionContextScope.h"
14#include "lldb/Target/StackFrame.h"
15#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 Clayton567e7f32011-09-22 04:58:26 +000022 m_target_sp (),
23 m_process_sp (),
24 m_thread_sp (),
25 m_frame_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000026{
27}
28
Greg Clayton567e7f32011-09-22 04:58:26 +000029ExecutionContext::ExecutionContext (const ExecutionContext &rhs) :
Greg Claytonb4d7fc02012-02-17 07:49:44 +000030 m_target_sp(rhs.m_target_sp),
Greg Clayton567e7f32011-09-22 04:58:26 +000031 m_process_sp(rhs.m_process_sp),
Greg Claytonb4d7fc02012-02-17 07:49:44 +000032 m_thread_sp(rhs.m_thread_sp),
33 m_frame_sp(rhs.m_frame_sp)
Greg Clayton567e7f32011-09-22 04:58:26 +000034{
35}
36
Greg Claytonb4d7fc02012-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 Clayton567e7f32011-09-22 04:58:26 +000042{
Greg Claytonb4d7fc02012-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
67ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
68 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 Clayton567e7f32011-09-22 04:58:26 +000075}
76
Greg Claytonf4124de2012-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
110ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
111 m_target_sp (),
112 m_process_sp (),
113 m_thread_sp (),
114 m_frame_sp ()
115{
116 lldb::StackFrameSP frame_sp(frame_wp.lock());
117 if (frame_sp)
118 SetContext (frame_sp);
119}
120
Chris Lattner24943d22010-06-08 16:52:24 +0000121ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
Greg Clayton13d24fb2012-01-29 20:56:30 +0000122 m_target_sp (t->shared_from_this()),
Greg Clayton567e7f32011-09-22 04:58:26 +0000123 m_process_sp (),
124 m_thread_sp (),
125 m_frame_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +0000126{
127 if (t && fill_current_process_thread_frame)
128 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000129 m_process_sp = t->GetProcessSP();
130 if (m_process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000131 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000132 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
133 if (m_thread_sp)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000134 m_frame_sp = m_thread_sp->GetSelectedFrame();
Chris Lattner24943d22010-06-08 16:52:24 +0000135 }
136 }
137}
138
Greg Clayton567e7f32011-09-22 04:58:26 +0000139ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
Greg Clayton13d24fb2012-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 Lattner24943d22010-06-08 16:52:24 +0000144{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000145 if (process)
146 m_target_sp = process->GetTarget().shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000147}
148
Greg Claytonb4d7fc02012-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
157ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr) :
158 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();
167 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
168 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
169 }
170}
171
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000172ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
173 m_target_sp (),
174 m_process_sp (),
175 m_thread_sp (),
176 m_frame_sp ()
177{
178 if (exe_ctx_ref_ptr)
179 {
180 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
181 if (m_target_sp)
182 {
183 locker.Lock(m_target_sp->GetAPIMutex());
184 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
185 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
186 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
187 }
188 }
189}
190
191ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
192 m_target_sp (exe_ctx_ref.GetTargetSP()),
193 m_process_sp (),
194 m_thread_sp (),
195 m_frame_sp ()
196{
197 if (m_target_sp)
198 {
199 locker.Lock(m_target_sp->GetAPIMutex());
200 m_process_sp = exe_ctx_ref.GetProcessSP();
201 m_thread_sp = exe_ctx_ref.GetThreadSP();
202 m_frame_sp = exe_ctx_ref.GetFrameSP();
203 }
204}
205
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000206ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
207 m_target_sp (),
208 m_process_sp (),
209 m_thread_sp (),
210 m_frame_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +0000211{
212 if (exe_scope_ptr)
Greg Claytona830adb2010-10-04 01:05:56 +0000213 exe_scope_ptr->CalculateExecutionContext (*this);
Chris Lattner24943d22010-06-08 16:52:24 +0000214}
215
216ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
217{
Greg Claytona830adb2010-10-04 01:05:56 +0000218 exe_scope_ref.CalculateExecutionContext (*this);
Chris Lattner24943d22010-06-08 16:52:24 +0000219}
220
221void
222ExecutionContext::Clear()
223{
Greg Clayton567e7f32011-09-22 04:58:26 +0000224 m_target_sp.reset();
225 m_process_sp.reset();
226 m_thread_sp.reset();
227 m_frame_sp.reset();
228}
229
230ExecutionContext::~ExecutionContext()
231{
Chris Lattner24943d22010-06-08 16:52:24 +0000232}
233
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000234uint32_t
235ExecutionContext::GetAddressByteSize() const
236{
237 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
238 m_target_sp->GetArchitecture().GetAddressByteSize();
239 if (m_process_sp)
240 m_process_sp->GetAddressByteSize();
241 return sizeof(void *);
242}
243
244
Chris Lattner24943d22010-06-08 16:52:24 +0000245
246RegisterContext *
247ExecutionContext::GetRegisterContext () const
248{
Greg Clayton567e7f32011-09-22 04:58:26 +0000249 if (m_frame_sp)
250 return m_frame_sp->GetRegisterContext().get();
251 else if (m_thread_sp)
252 return m_thread_sp->GetRegisterContext().get();
253 return NULL;
254}
255
256Target *
257ExecutionContext::GetTargetPtr () const
258{
259 if (m_target_sp)
260 return m_target_sp.get();
261 if (m_process_sp)
262 return &m_process_sp->GetTarget();
263 return NULL;
264}
265
266Process *
267ExecutionContext::GetProcessPtr () const
268{
269 if (m_process_sp)
270 return m_process_sp.get();
271 if (m_target_sp)
272 return m_target_sp->GetProcessSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000273 return NULL;
274}
275
276ExecutionContextScope *
277ExecutionContext::GetBestExecutionContextScope () const
278{
Greg Clayton567e7f32011-09-22 04:58:26 +0000279 if (m_frame_sp)
280 return m_frame_sp.get();
281 if (m_thread_sp)
282 return m_thread_sp.get();
283 if (m_process_sp)
284 return m_process_sp.get();
285 return m_target_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000286}
Greg Clayton801417e2011-07-07 01:59:51 +0000287
Greg Clayton567e7f32011-09-22 04:58:26 +0000288Target &
289ExecutionContext::GetTargetRef () const
Greg Clayton801417e2011-07-07 01:59:51 +0000290{
Greg Claytonf4124de2012-02-21 00:09:25 +0000291#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000292 assert (m_target_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000293#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000294 return *m_target_sp;
Greg Clayton801417e2011-07-07 01:59:51 +0000295}
Greg Clayton567e7f32011-09-22 04:58:26 +0000296
297Process &
298ExecutionContext::GetProcessRef () const
299{
Greg Claytonf4124de2012-02-21 00:09:25 +0000300#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000301 assert (m_process_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000302#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000303 return *m_process_sp;
304}
305
306Thread &
307ExecutionContext::GetThreadRef () const
308{
Greg Claytonf4124de2012-02-21 00:09:25 +0000309#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000310 assert (m_thread_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000311#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000312 return *m_thread_sp;
313}
314
315StackFrame &
316ExecutionContext::GetFrameRef () const
317{
Greg Claytonf4124de2012-02-21 00:09:25 +0000318#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000319 assert (m_frame_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000320#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000321 return *m_frame_sp;
322}
323
324void
325ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
326{
327 m_target_sp = target_sp;
328}
329
330void
331ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
332{
333 m_process_sp = process_sp;
334}
335
336void
337ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
338{
339 m_thread_sp = thread_sp;
340}
341
342void
343ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
344{
345 m_frame_sp = frame_sp;
346}
347
348void
349ExecutionContext::SetTargetPtr (Target* target)
350{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000351 if (target)
352 m_target_sp = target->shared_from_this();
353 else
354 m_target_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000355}
356
357void
358ExecutionContext::SetProcessPtr (Process *process)
359{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000360 if (process)
361 m_process_sp = process->shared_from_this();
362 else
363 m_process_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000364}
365
366void
367ExecutionContext::SetThreadPtr (Thread *thread)
368{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000369 if (thread)
370 m_thread_sp = thread->shared_from_this();
371 else
372 m_thread_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000373}
374
375void
376ExecutionContext::SetFramePtr (StackFrame *frame)
377{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000378 if (frame)
379 m_frame_sp = frame->shared_from_this();
380 else
381 m_frame_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000382}
383
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000384void
385ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
386{
387 m_target_sp = target_sp;
388 if (get_process && target_sp)
389 m_process_sp = target_sp->GetProcessSP();
390 else
391 m_process_sp.reset();
392 m_thread_sp.reset();
393 m_frame_sp.reset();
394}
395
396void
397ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
398{
399 m_process_sp = process_sp;
400 if (process_sp)
401 m_target_sp = process_sp->GetTarget().shared_from_this();
402 else
403 m_target_sp.reset();
404 m_thread_sp.reset();
405 m_frame_sp.reset();
406}
407
408void
409ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
410{
411 m_frame_sp.reset();
412 m_thread_sp = thread_sp;
413 if (thread_sp)
414 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000415 m_process_sp = thread_sp->GetProcess();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000416 if (m_process_sp)
417 m_target_sp = m_process_sp->GetTarget().shared_from_this();
418 else
419 m_target_sp.reset();
420 }
421 else
422 {
423 m_target_sp.reset();
424 m_process_sp.reset();
425 }
426}
427
428void
429ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
430{
431 m_frame_sp = frame_sp;
432 if (frame_sp)
433 {
Greg Clayton289afcb2012-02-18 05:35:26 +0000434 m_thread_sp = frame_sp->CalculateThread();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000435 if (m_thread_sp)
436 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000437 m_process_sp = m_thread_sp->GetProcess();
Greg Claytonb4d7fc02012-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 else
450 {
451 m_target_sp.reset();
452 m_process_sp.reset();
453 m_thread_sp.reset();
454 }
455}
456
457ExecutionContext &
458ExecutionContext::operator =(const ExecutionContext &rhs)
459{
460 if (this != &rhs)
461 {
462 m_target_sp = rhs.m_target_sp;
463 m_process_sp = rhs.m_process_sp;
464 m_thread_sp = rhs.m_thread_sp;
465 m_frame_sp = rhs.m_frame_sp;
466 }
467 return *this;
468}
469
470bool
471ExecutionContext::operator ==(const ExecutionContext &rhs) const
472{
473 // Check that the frame shared pointers match, or both are valid and their stack
474 // IDs match since sometimes we get new objects that represent the same
475 // frame within a thread.
476 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
477 {
478 // Check that the thread shared pointers match, or both are valid and
479 // their thread IDs match since sometimes we get new objects that
480 // represent the same thread within a process.
481 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
482 {
483 // Processes and targets don't change much
484 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
485 }
486 }
487 return false;
488}
489
490bool
491ExecutionContext::operator !=(const ExecutionContext &rhs) const
492{
493 return !(*this == rhs);
494}
495
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000496bool
497ExecutionContext::HasTargetScope () const
498{
499 return ((bool) m_target_sp
500 && m_target_sp->IsValid());
501}
502
503bool
504ExecutionContext::HasProcessScope () const
505{
506 return (HasTargetScope()
507 && ((bool) m_process_sp && m_process_sp->IsValid()));
508}
509
510bool
511ExecutionContext::HasThreadScope () const
512{
513 return (HasProcessScope()
514 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
515}
516
517bool
518ExecutionContext::HasFrameScope () const
519{
520 return HasThreadScope() && m_frame_sp;
521}
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000522
523ExecutionContextRef::ExecutionContextRef() :
524 m_target_wp (),
525 m_process_wp (),
526 m_thread_wp (),
527 m_frame_wp (),
528 m_tid(LLDB_INVALID_THREAD_ID),
529 m_stack_id ()
530{
531}
532
533ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
534 m_target_wp (),
535 m_process_wp (),
536 m_thread_wp (),
537 m_frame_wp (),
538 m_tid(LLDB_INVALID_THREAD_ID),
539 m_stack_id ()
540{
541 if (exe_ctx)
542 *this = *exe_ctx;
543}
544
545ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
546 m_target_wp (),
547 m_process_wp (),
548 m_thread_wp (),
549 m_frame_wp (),
550 m_tid(LLDB_INVALID_THREAD_ID),
551 m_stack_id ()
552{
553 *this = exe_ctx;
554}
555
556
557ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
558 m_target_wp(),
559 m_process_wp(),
560 m_thread_wp(),
561 m_frame_wp(),
562 m_tid(LLDB_INVALID_THREAD_ID),
563 m_stack_id ()
564{
565 SetTargetPtr (target, adopt_selected);
566}
567
568
569
570
571ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
572 m_target_wp (rhs.m_target_wp),
573 m_process_wp(rhs.m_process_wp),
574 m_thread_wp (rhs.m_thread_wp),
575 m_frame_wp (rhs.m_frame_wp),
576 m_tid (rhs.m_tid),
577 m_stack_id (rhs.m_stack_id)
578{
579}
580
581ExecutionContextRef &
582ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
583{
584 if (this != &rhs)
585 {
586 m_target_wp = rhs.m_target_wp;
587 m_process_wp = rhs.m_process_wp;
588 m_thread_wp = rhs.m_thread_wp;
589 m_frame_wp = rhs.m_frame_wp;
590 m_tid = rhs.m_tid;
591 m_stack_id = rhs.m_stack_id;
592 }
593 return *this;
594}
595
596ExecutionContextRef &
597ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
598{
Greg Claytona894fe72012-04-05 16:12:35 +0000599 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000600 m_process_wp = exe_ctx.GetProcessSP();
Greg Claytona894fe72012-04-05 16:12:35 +0000601 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
602 m_thread_wp = thread_sp;
603 if (thread_sp)
604 m_tid = thread_sp->GetID();
605 else
606 m_tid = LLDB_INVALID_THREAD_ID;
607 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
608 m_frame_wp = frame_sp;
609 if (frame_sp)
610 m_stack_id = frame_sp->GetStackID();
611 else
612 m_stack_id.Clear();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000613 return *this;
614}
615
616void
617ExecutionContextRef::Clear()
618{
619 m_target_wp.reset();
620 m_process_wp.reset();
Greg Claytona894fe72012-04-05 16:12:35 +0000621 ClearThread();
622 ClearFrame();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000623}
624
625ExecutionContextRef::~ExecutionContextRef()
626{
627}
628
629void
630ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
631{
632 m_target_wp = target_sp;
633}
634
635void
636ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
637{
Greg Claytona894fe72012-04-05 16:12:35 +0000638 if (process_sp)
639 {
640 m_process_wp = process_sp;
641 SetTargetSP (process_sp->GetTarget().shared_from_this());
642 }
643 else
644 {
645 m_process_wp.reset();
646 m_target_wp.reset();
647 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000648}
649
650void
651ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
652{
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000653 if (thread_sp)
Greg Claytona894fe72012-04-05 16:12:35 +0000654 {
655 m_thread_wp = thread_sp;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000656 m_tid = thread_sp->GetID();
Greg Claytona894fe72012-04-05 16:12:35 +0000657 SetProcessSP (thread_sp->GetProcess());
658 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000659 else
Greg Claytona894fe72012-04-05 16:12:35 +0000660 {
661 ClearThread();
662 m_process_wp.reset();
663 m_target_wp.reset();
664 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000665}
666
667void
668ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
669{
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000670 if (frame_sp)
Greg Claytona894fe72012-04-05 16:12:35 +0000671 {
672 m_frame_wp = frame_sp;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000673 m_stack_id = frame_sp->GetStackID();
Greg Claytona894fe72012-04-05 16:12:35 +0000674 SetThreadSP (frame_sp->GetThread());
675 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000676 else
Greg Claytona894fe72012-04-05 16:12:35 +0000677 {
678 ClearFrame();
679 ClearThread();
680 m_process_wp.reset();
681 m_target_wp.reset();
682 }
683
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000684}
685
686void
687ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
688{
689 Clear();
690 if (target)
691 {
692 lldb::TargetSP target_sp (target->shared_from_this());
693 if (target_sp)
694 {
695 m_target_wp = target_sp;
696 if (adopt_selected)
697 {
698 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
699 if (process_sp)
700 {
701 m_process_wp = process_sp;
702 if (process_sp)
703 {
Greg Claytond8ae7f22012-07-30 22:05:39 +0000704 // Only fill in the thread and frame if our process is stopped
705 if (StateIsStoppedState (process_sp->GetState(), true))
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000706 {
Greg Claytond8ae7f22012-07-30 22:05:39 +0000707 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
708 if (!thread_sp)
709 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
710
711 if (thread_sp)
712 {
713 SetThreadSP (thread_sp);
714 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
715 if (!frame_sp)
716 frame_sp = thread_sp->GetStackFrameAtIndex(0);
717 if (frame_sp)
718 SetFrameSP (frame_sp);
719 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000720 }
721 }
722 }
723 }
724 }
725 }
726}
727
728void
729ExecutionContextRef::SetProcessPtr (Process *process)
730{
731 if (process)
Greg Claytona894fe72012-04-05 16:12:35 +0000732 {
733 SetProcessSP(process->shared_from_this());
734 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000735 else
Greg Claytona894fe72012-04-05 16:12:35 +0000736 {
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000737 m_process_wp.reset();
Greg Claytona894fe72012-04-05 16:12:35 +0000738 m_target_wp.reset();
739 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000740}
741
742void
743ExecutionContextRef::SetThreadPtr (Thread *thread)
744{
745 if (thread)
Greg Claytona894fe72012-04-05 16:12:35 +0000746 {
747 SetThreadSP (thread->shared_from_this());
748 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000749 else
Greg Claytona894fe72012-04-05 16:12:35 +0000750 {
751 ClearThread();
752 m_process_wp.reset();
753 m_target_wp.reset();
754 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000755}
756
757void
758ExecutionContextRef::SetFramePtr (StackFrame *frame)
759{
760 if (frame)
Greg Claytona894fe72012-04-05 16:12:35 +0000761 SetFrameSP (frame->shared_from_this());
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000762 else
Greg Claytona894fe72012-04-05 16:12:35 +0000763 Clear();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000764}
765
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000766lldb::TargetSP
767ExecutionContextRef::GetTargetSP () const
768{
769 lldb::TargetSP target_sp(m_target_wp.lock());
770 if (target_sp && !target_sp->IsValid())
771 target_sp.reset();
772 return target_sp;
773}
774
775lldb::ProcessSP
776ExecutionContextRef::GetProcessSP () const
777{
778 lldb::ProcessSP process_sp(m_process_wp.lock());
779 if (process_sp && !process_sp->IsValid())
780 process_sp.reset();
781 return process_sp;
782}
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000783
784lldb::ThreadSP
785ExecutionContextRef::GetThreadSP () const
786{
787 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000788
Greg Clayton3feafab2012-04-04 20:43:47 +0000789 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000790 {
Greg Clayton3feafab2012-04-04 20:43:47 +0000791 // We check if the thread has been destroyed in cases where clients
792 // might still have shared pointer to a thread, but the thread is
793 // not valid anymore (not part of the process)
794 if (!thread_sp || !thread_sp->IsValid())
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000795 {
Greg Clayton3feafab2012-04-04 20:43:47 +0000796 lldb::ProcessSP process_sp(GetProcessSP());
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000797 if (process_sp && process_sp->IsValid())
Greg Clayton3feafab2012-04-04 20:43:47 +0000798 {
799 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
800 m_thread_wp = thread_sp;
801 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000802 }
803 }
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000804
805 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
806 // but don't return an invalid one.
807
808 if (thread_sp && !thread_sp->IsValid())
809 thread_sp.reset();
810
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000811 return thread_sp;
812}
813
814lldb::StackFrameSP
815ExecutionContextRef::GetFrameSP () const
816{
817 lldb::StackFrameSP frame_sp (m_frame_wp.lock());
Enrico Granata41b5bfe2013-03-15 17:25:04 +0000818 if (frame_sp)
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000819 {
Enrico Granata41b5bfe2013-03-15 17:25:04 +0000820 lldb::ThreadSP frame_thread_sp(frame_sp->GetThread());
821 if (frame_thread_sp && frame_thread_sp->IsValid())
822 return frame_sp;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000823 }
Enrico Granata41b5bfe2013-03-15 17:25:04 +0000824 lldb::ThreadSP thread_sp (GetThreadSP());
825 if (thread_sp && thread_sp->IsValid())
826 {
827 frame_sp = thread_sp->GetFrameWithStackID (m_stack_id);
828 m_frame_wp = frame_sp;
829 return frame_sp;
830 }
831 return lldb::StackFrameSP();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000832}
833
834ExecutionContext
835ExecutionContextRef::Lock () const
836{
837 return ExecutionContext(this);
838}
839
840