blob: 8b5731e80280d9d9335481feaf98f85bf7ee3429 [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 (),
Greg Clayton1e911a02013-03-15 23:54:07 +0000527 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000528 m_stack_id ()
529{
530}
531
532ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
533 m_target_wp (),
534 m_process_wp (),
535 m_thread_wp (),
Greg Clayton1e911a02013-03-15 23:54:07 +0000536 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000537 m_stack_id ()
538{
539 if (exe_ctx)
540 *this = *exe_ctx;
541}
542
543ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
544 m_target_wp (),
545 m_process_wp (),
546 m_thread_wp (),
Greg Clayton1e911a02013-03-15 23:54:07 +0000547 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000548 m_stack_id ()
549{
550 *this = exe_ctx;
551}
552
553
554ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
555 m_target_wp(),
556 m_process_wp(),
557 m_thread_wp(),
Greg Clayton1e911a02013-03-15 23:54:07 +0000558 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000559 m_stack_id ()
560{
561 SetTargetPtr (target, adopt_selected);
562}
563
564
565
566
567ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
568 m_target_wp (rhs.m_target_wp),
569 m_process_wp(rhs.m_process_wp),
570 m_thread_wp (rhs.m_thread_wp),
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000571 m_tid (rhs.m_tid),
572 m_stack_id (rhs.m_stack_id)
573{
574}
575
576ExecutionContextRef &
577ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
578{
579 if (this != &rhs)
580 {
581 m_target_wp = rhs.m_target_wp;
582 m_process_wp = rhs.m_process_wp;
583 m_thread_wp = rhs.m_thread_wp;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000584 m_tid = rhs.m_tid;
585 m_stack_id = rhs.m_stack_id;
586 }
587 return *this;
588}
589
590ExecutionContextRef &
591ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
592{
Greg Claytona894fe72012-04-05 16:12:35 +0000593 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000594 m_process_wp = exe_ctx.GetProcessSP();
Greg Claytona894fe72012-04-05 16:12:35 +0000595 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
596 m_thread_wp = thread_sp;
597 if (thread_sp)
598 m_tid = thread_sp->GetID();
599 else
600 m_tid = LLDB_INVALID_THREAD_ID;
601 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Claytona894fe72012-04-05 16:12:35 +0000602 if (frame_sp)
603 m_stack_id = frame_sp->GetStackID();
604 else
605 m_stack_id.Clear();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000606 return *this;
607}
608
609void
610ExecutionContextRef::Clear()
611{
612 m_target_wp.reset();
613 m_process_wp.reset();
Greg Claytona894fe72012-04-05 16:12:35 +0000614 ClearThread();
615 ClearFrame();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000616}
617
618ExecutionContextRef::~ExecutionContextRef()
619{
620}
621
622void
623ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
624{
625 m_target_wp = target_sp;
626}
627
628void
629ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
630{
Greg Claytona894fe72012-04-05 16:12:35 +0000631 if (process_sp)
632 {
633 m_process_wp = process_sp;
634 SetTargetSP (process_sp->GetTarget().shared_from_this());
635 }
636 else
637 {
638 m_process_wp.reset();
639 m_target_wp.reset();
640 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000641}
642
643void
644ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
645{
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000646 if (thread_sp)
Greg Claytona894fe72012-04-05 16:12:35 +0000647 {
648 m_thread_wp = thread_sp;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000649 m_tid = thread_sp->GetID();
Greg Claytona894fe72012-04-05 16:12:35 +0000650 SetProcessSP (thread_sp->GetProcess());
651 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000652 else
Greg Claytona894fe72012-04-05 16:12:35 +0000653 {
654 ClearThread();
655 m_process_wp.reset();
656 m_target_wp.reset();
657 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000658}
659
660void
661ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
662{
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000663 if (frame_sp)
Greg Claytona894fe72012-04-05 16:12:35 +0000664 {
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000665 m_stack_id = frame_sp->GetStackID();
Greg Claytona894fe72012-04-05 16:12:35 +0000666 SetThreadSP (frame_sp->GetThread());
667 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000668 else
Greg Claytona894fe72012-04-05 16:12:35 +0000669 {
670 ClearFrame();
671 ClearThread();
672 m_process_wp.reset();
673 m_target_wp.reset();
674 }
675
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000676}
677
678void
679ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
680{
681 Clear();
682 if (target)
683 {
684 lldb::TargetSP target_sp (target->shared_from_this());
685 if (target_sp)
686 {
687 m_target_wp = target_sp;
688 if (adopt_selected)
689 {
690 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
691 if (process_sp)
692 {
693 m_process_wp = process_sp;
694 if (process_sp)
695 {
Greg Claytond8ae7f22012-07-30 22:05:39 +0000696 // Only fill in the thread and frame if our process is stopped
697 if (StateIsStoppedState (process_sp->GetState(), true))
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000698 {
Greg Claytond8ae7f22012-07-30 22:05:39 +0000699 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
700 if (!thread_sp)
701 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
702
703 if (thread_sp)
704 {
705 SetThreadSP (thread_sp);
706 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
707 if (!frame_sp)
708 frame_sp = thread_sp->GetStackFrameAtIndex(0);
709 if (frame_sp)
710 SetFrameSP (frame_sp);
711 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000712 }
713 }
714 }
715 }
716 }
717 }
718}
719
720void
721ExecutionContextRef::SetProcessPtr (Process *process)
722{
723 if (process)
Greg Claytona894fe72012-04-05 16:12:35 +0000724 {
725 SetProcessSP(process->shared_from_this());
726 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000727 else
Greg Claytona894fe72012-04-05 16:12:35 +0000728 {
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000729 m_process_wp.reset();
Greg Claytona894fe72012-04-05 16:12:35 +0000730 m_target_wp.reset();
731 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000732}
733
734void
735ExecutionContextRef::SetThreadPtr (Thread *thread)
736{
737 if (thread)
Greg Claytona894fe72012-04-05 16:12:35 +0000738 {
739 SetThreadSP (thread->shared_from_this());
740 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000741 else
Greg Claytona894fe72012-04-05 16:12:35 +0000742 {
743 ClearThread();
744 m_process_wp.reset();
745 m_target_wp.reset();
746 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000747}
748
749void
750ExecutionContextRef::SetFramePtr (StackFrame *frame)
751{
752 if (frame)
Greg Claytona894fe72012-04-05 16:12:35 +0000753 SetFrameSP (frame->shared_from_this());
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000754 else
Greg Claytona894fe72012-04-05 16:12:35 +0000755 Clear();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000756}
757
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000758lldb::TargetSP
759ExecutionContextRef::GetTargetSP () const
760{
761 lldb::TargetSP target_sp(m_target_wp.lock());
762 if (target_sp && !target_sp->IsValid())
763 target_sp.reset();
764 return target_sp;
765}
766
767lldb::ProcessSP
768ExecutionContextRef::GetProcessSP () const
769{
770 lldb::ProcessSP process_sp(m_process_wp.lock());
771 if (process_sp && !process_sp->IsValid())
772 process_sp.reset();
773 return process_sp;
774}
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000775
776lldb::ThreadSP
777ExecutionContextRef::GetThreadSP () const
778{
779 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000780
Greg Clayton3feafab2012-04-04 20:43:47 +0000781 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000782 {
Greg Clayton3feafab2012-04-04 20:43:47 +0000783 // We check if the thread has been destroyed in cases where clients
784 // might still have shared pointer to a thread, but the thread is
785 // not valid anymore (not part of the process)
786 if (!thread_sp || !thread_sp->IsValid())
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000787 {
Greg Clayton3feafab2012-04-04 20:43:47 +0000788 lldb::ProcessSP process_sp(GetProcessSP());
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000789 if (process_sp && process_sp->IsValid())
Greg Clayton3feafab2012-04-04 20:43:47 +0000790 {
791 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
792 m_thread_wp = thread_sp;
793 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000794 }
795 }
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000796
797 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
798 // but don't return an invalid one.
799
800 if (thread_sp && !thread_sp->IsValid())
801 thread_sp.reset();
802
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000803 return thread_sp;
804}
805
806lldb::StackFrameSP
807ExecutionContextRef::GetFrameSP () const
808{
Greg Claytond1ddde02013-05-24 00:58:29 +0000809 if (m_stack_id.IsValid())
810 {
811 lldb::ThreadSP thread_sp (GetThreadSP());
812 if (thread_sp)
813 return thread_sp->GetFrameWithStackID (m_stack_id);
814 }
Enrico Granata41b5bfe2013-03-15 17:25:04 +0000815 return lldb::StackFrameSP();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000816}
817
818ExecutionContext
819ExecutionContextRef::Lock () const
820{
821 return ExecutionContext(this);
822}
823
824